0% found this document useful (0 votes)
10 views28 pages

9 Function - Part 2 2022 - EDI3183

Uploaded by

diva8287450
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views28 pages

9 Function - Part 2 2022 - EDI3183

Uploaded by

diva8287450
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

9.

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 max (int x, int y);


• Here, parameters x and y are value parameters
• When you call the max function as max(4, 7), the values 4 and 7 are copied to x and y
respectively
• When you call the max function as max (a, b), where a=40 and b=10, the values 40 and 10
are copied to x and y respectively
• When you call the max function as max( a+b, b/2), the values 50 and 5 are copies to x and
y respectively

• Once the value parameters accepted copies of the corresponding arguments


data, they act as local variables!
13
Example of Using Value Parameters
and Global Variables
#include <iostream> x 0
using namespace std;
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
int main()
{ int main()
x = 4; {
1 x = 4;
fun(x/2+1); fun(x/2+1);
cout << x << endl; cout << x << endl;
} } 14
Example of Using Value Parameters and
Global Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
void fun(int x)
{
cout << x << endl; void fun(int x )
{
x=x+5;
3 cout << x << endl;
} x=x+5;
int main() }
{
x = 4; int main()
fun(x/2+1); {
x = 4; 3
cout << x << endl;
2 fun(x/2+1);
} cout << x << endl;
} 15
Example of Using Value Parameters and
Global Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
void fun(int x)
{
cout << x << endl; void fun(int x 8
3 )
{
x=x+5;
cout << x << endl;
} 4 x=x+5;
int main() }
{
x = 4; int main()
fun(x/2+1); {
x = 4;
cout << x << endl;
2 fun(x/2+1);
} cout << x << endl;
} 16
Example of Using Value Parameters and
Global Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
void fun(int x)
{
cout << x << endl; void fun(int x 3
8 )
{
x=x+5;
cout << x << endl;
} x=x+5;
int main() 5 }
{
x = 4; int main()
fun(x/2+1); {
x = 4;
cout << x << endl;
2 fun(x/2+1);
} cout << x << endl;
} 17
Example of Using Value Parameters and
Global Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
int main()
{
x = 4; int main()
fun(x/2+1); {
x = 4;
cout << x << endl;
fun(x/2+1);
} 6 cout << x << endl;
} 18
Example of Using Value Parameters and
Global Variables
#include <iostream> x 4
using namespace std;
int x; // Global variable
void fun(int x)
{
cout << x << endl;
x=x+5;
}
int main()
{
x = 4; int main()
fun(x/2+1); {
x = 4;
cout << x << endl;
fun(x/2+1);
} cout << x << endl;
7 } 19
Reference Parameters
• As we saw in the last example, any changes in the value
parameters don’t affect the original function arguments
• Sometimes, we want to change the values of the original
function arguments or return with more than one value
from the function, in this case we use reference
parameters
• A reference parameter is just another name to the original
argument variable
• We define a reference parameter by adding the & in front of the
parameter name, e.g.
double update (double & x);
20
Example of Reference Parameters
#include <iostream>
using namespace std;
void fun(int &y)
{
cout << y << endl;
y=y+5;
}
int main()
{
int x = 4; // Local variable int main()
{
fun(x); ?
4 x
1 int x = 4;
cout << x << endl; fun(x);
} cout << x << endl;
}
21
Example of Reference Parameters
#include <iostream>
using namespace std;
void fun(int &y)
{
void fun( int & y )
cout << y << endl; {
y=y+5; 3 cout<<y<<endl;
} y=y+5;
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;
}
}
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

You might also like