9 Function - Part 2 2022 - EDI3183
9 Function - Part 2 2022 - EDI3183
Function
EDI3183 BASIC PROGRAMMING FOR TECHNOLOGIST
SEMESTER 1 2022/2023
II. Local Variables
• Local variables are declared inside the function body and
exist as long as the function is running and destroyed
when the function exit
• You have to initialize the local variable before using it
• If a function defines a local variable and there was a
global variable with the same name, the function uses its
local variable instead of using the global variable
2
Example of Defining and Using
Global and Local Variables
#include <iostream>
using namespace std;
int x; // Global variable
void fun(); // function prototype
int main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
} 3
Example of Defining and Using
Global and Local Variables
#include <iostream> x 0
using namespace std;
int x; // Global variable Global variables are
Void fun(); // function prototype automatically initialized to 0
int main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
4
}
Example of Defining and Using
Global and Local Variables
#include <iostream> x 0
using namespace std;
int x; // Global variable
Void fun(); // function prototype
int main()
{
x = 4;
fun();
cout << x << endl;
}
int main()
void fun() {
{ 1 x = 4;
int x = 10; // Local variable fun();
cout << x << endl; cout << x << endl;
}
} 5
Example of Defining and Using
Global and Local Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
Void fun(); // function prototype
void fun()
int main()
{ x ????
x = 4; {
fun(); 3 int x = 10;
cout << x << endl; cout << x << endl;
} }
int main()
void fun() {
{ x = 4;
int x = 10; // Local variable 2 fun();
cout << x << endl; cout << x << endl;
} } 6
Example of Defining and Using
Global and Local Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
Void fun(); // function prototype
void fun()
int main()
x 10
{
x = 4; {
fun(); 3 int x = 10;
cout << x << endl;
cout << x << endl;
}
}
int main()
void fun() {
{ x = 4;
int x = 10; // Local variable 2 fun();
cout << x << endl; cout << x << endl;
}
} 7
Example of Defining and Using Global and
Local Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
Void fun(); // function prototype
void fun()
int main()
{ x 10
x = 4;
{
fun();
int x = 10;
cout << x << endl;
} 4 cout << x << endl;
}
void fun()
int main()
{
{
int x = 10; // Local variable
x = 4;
cout << x << endl;
2 fun();
}
cout << x << endl;
} 8
Example of Defining and Using Global and
Local Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
Void fun(); // function signature
void fun()
int main()
{ x 10
x = 4;
{
fun();
int x = 10;
cout << x << endl;
}
cout << x << endl;
5 }
void fun()
int main()
{
{
int x = 10; // Local variable
x = 4;
cout << x << endl;
2 fun();
}
cout << x << endl;
} 9
Example of Defining and Using Global and
Local Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
Void fun(); // function signature
int main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
int main()
{
{
int x = 10; // Local variable
x = 4;
cout << x << endl;
fun();
}
6 cout << x << endl;
} 10
Example of Defining and Using Global and
Local Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
Void fun(); // function prototype
int main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
int main()
{
{
int x = 10; // Local variable
x = 4;
cout << x << endl;
fun();
}
cout << x << endl;
7 } 11
III. Using Function Parameters
• Function Parameters come in three flavors:
• Value parameters – which copy the values of the function
arguments
• Reference parameters – which refer to the function arguments
by other local names and have the ability to change the values
of the referenced arguments
• Constant reference parameters – similar to the reference
parameters but cannot change the values of the referenced
arguments
12
Value Parameters
• This is what we use to declare in the function prototype or function header,
e.g.
• <type> <function name>(<type list>);
{
int main()
int x = 4; // Local variable {
fun(x); int x = 4; ?
4 x
cout << x << endl; 2 fun(x);
cout << x << endl;
}
}
22
Example of Reference Parameters
#include <iostream>
using namespace std;
void fun(int &y)
{
void fun( int & y )
cout << y << endl; {
y=y+5; cout<<y<<endl;
} 4 y=y+5;
9
int main() }
{
int main()
int x = 4; // Local variable {
fun(x); int x = 4; ?
4 x
cout << x << endl; 2 fun(x);
cout << x << endl;
}
}
23
Example of Reference Parameters
#include <iostream>
using namespace std;
void fun(int &y)
{
void fun( int & y )
cout << y << endl; {
y=y+5; cout<<y<<endl;
} y=y+5;
int main() 5 }
{
int main()
int x = 4; // Local variable {
fun(x); int x = 4; ?
9 x
cout << x << endl; 2 fun(x);
cout << x << endl;
}
}
24
Example of Reference Parameters
#include <iostream>
using namespace std;
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
int main()
{
int main()
int x = 4; // Local variable {
fun(x); int x = 4; ?
9 x
cout << x << endl; fun(x);
6 cout << x << endl;
}
}
25
Example of Reference Parameters
#include <iostream>
using namespace std;
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
int main()
{
int main()
int x = 4; // Local variable {
fun(x); int x = 4; ?
9 x
cout << x << endl; fun(x);
cout << x << endl;
}
7 }
26
Constant Reference Parameters
• Constant reference parameters are used under the following two
conditions:
• The passed data are so big and you want to save time and computer memory
• The passed data will not be changed or updated in the function body
• For example
void fun (const int & y);
• It enlists the compilers help in ensuring values that shouldn’t be
changed aren’t changed (the compiler will throw an error if you try, like
in the above example).
• It tells the programmer that the function won’t change the value of the
argument. This can help with debugging.
• You can’t pass a const argument to a non-const reference parameter.
Using const parameters ensures you can pass both non-const and const
arguments to the function.
27
• Const references can accept any type of argument
Constant Reference Parameters
#include <iostream>
using namespace std;
void fun(const int &y)
{
cout << y << endl;
y=y+5;
}
int main()
{
int x = 4; // Local variable
fun(x);
cout << x << endl;
}
28