SlideShare a Scribd company logo
1
1
Example #2
Example #2
 Write a function to compute the distance
Write a function to compute the distance
between two points (x1, y1) and (x2, y2)
between two points (x1, y1) and (x2, y2)
Inline double distance (double x1, double y1,
Inline double distance (double x1, double y1,
double x2, double y2)
double x2, double y2)
{
{
return sqrt(pow(x1-x2,2)+pow(y1-y2,2));
return sqrt(pow(x1-x2,2)+pow(y1-y2,2));
}
}
2
2
Example #3
Example #3
 Write a function to compute n!
Write a function to compute n!
int factorial( int n)
int factorial( int n)
{
{
int product=1;
int product=1;
for (int i=1; i<=n; i++) product *= i;
for (int i=1; i<=n; i++) product *= i;
return product;
return product;
}
}
3
3
Example #4
Example #4
Function Overloading
Function Overloading
 Write functions to return with the maximum number of
Write functions to return with the maximum number of
two numbers
two numbers
inline int max( int x, int y)
inline int max( int x, int y)
{
{
if (x>y) return x; else return y;
if (x>y) return x; else return y;
}
}
inline double max( double x, double y)
inline double max( double x, double y)
{
{
if (x>y) return x; else return y;
if (x>y) return x; else return y;
}
}
An overloaded
function is a
function that is
defined more than
once with different
data types or
different number of
parameters
4
4
Sharing Data Among
Sharing Data Among
User-Defined Functions
User-Defined Functions
There are two ways to share data
There are two ways to share data
among different functions
among different functions
Using global variables (very bad practice!)
Using global variables (very bad practice!)
Passing data through function parameters
Passing data through function parameters
Value parameters
Value parameters
Reference parameters
Reference parameters
Constant reference parameters
Constant reference parameters
5
5
C++ Variables
C++ Variables
 A variable is a place in memory that has
A variable is a place in memory that has
 A name or identifier (e.g. income, taxes, etc.)
A name or identifier (e.g. income, taxes, etc.)
 A data type (e.g. int, double, char, etc.)
A data type (e.g. int, double, char, etc.)
 A size (number of bytes)
A size (number of bytes)
 A scope (the part of the program code that can use it)
A scope (the part of the program code that can use it)
 Global variables – all functions can see it and using it
Global variables – all functions can see it and using it
 Local variables – only the function that declare local
Local variables – only the function that declare local
variables see and use these variables
variables see and use these variables
 A life time (the duration of its existence)
A life time (the duration of its existence)
 Global variables can live as long as the program is executed
Global variables can live as long as the program is executed
 Local variables are lived only when the functions that define
Local variables are lived only when the functions that define
these variables are executed
these variables are executed
6
6
I. Using Global Variables
I. Using Global Variables
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
void f1() { x++; }
void f1() { x++; }
void f2() { x+=4; f1(); }
void f2() { x+=4; f1(); }
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
7
7
I. Using Global Variables
I. Using Global Variables
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
void f1() { x++; }
void f1() { x++; }
void f2() { x+=4; f1(); }
void f2() { x+=4; f1(); }
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
x 0
8
8
I. Using Global Variables
I. Using Global Variables
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
void f1() { x++; }
void f1() { x++; }
void f2() { x+=4; f1(); }
void f2() { x+=4; f1(); }
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
x 0
void main()
void main()
{
{
f2();
f2();
cout << x << endl ;
cout << x << endl ;
}
}
1
9
9
I. Using Global Variables
I. Using Global Variables
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
void f1() { x++; }
void f1() { x++; }
void f2() { x+=4; f1(); }
void f2() { x+=4; f1(); }
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
x 0
void main()
void main()
{
{
f2();
f2();
cout << x << endl ;
cout << x << endl ;
}
}
1
void f2()
void f2()
{
{
x += 4;
x += 4;
f1();
f1();
}
}
2
4
10
10
4
5
I. Using Global Variables
I. Using Global Variables
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
void f1() { x++; }
void f1() { x++; }
void f2() { x+=4; f1(); }
void f2() { x+=4; f1(); }
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
x
void main()
void main()
{
{
f2();
f2();
cout << x << endl ;
cout << x << endl ;
}
}
1
void f2()
void f2()
{
{
x += 4;
x += 4;
f1();
f1();
}
}
3
void f1()
void f1()
{
{
x++;
x++;
}
}
4
11
11
4
5
I. Using Global Variables
I. Using Global Variables
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
void f1() { x++; }
void f1() { x++; }
void f2() { x+=4; f1(); }
void f2() { x+=4; f1(); }
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
x
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
1
void f2()
void f2()
{
{
x += 4;
x += 4;
f1();
f1();
}
}
3
void f1()
void f1()
{
{
x++;
x++;
}
}
5
12
12
4
5
I. Using Global Variables
I. Using Global Variables
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
void f1() { x++; }
void f1() { x++; }
void f2() { x+=4; f1(); }
void f2() { x+=4; f1(); }
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
x
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
1
void f2()
void f2()
{
{
x += 4;
x += 4;
f1();
f1();
}
}
6
13
13
4
5
I. Using Global Variables
I. Using Global Variables
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
void f1() { x++; }
void f1() { x++; }
void f2() { x+=4; f1(); }
void f2() { x+=4; f1(); }
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
x
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
7
14
14
4
5
I. Using Global Variables
I. Using Global Variables
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
void f1() { x++; }
void f1() { x++; }
void f2() { x+=4; f1(); }
void f2() { x+=4; f1(); }
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
x
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
8
15
15
I. Using Global Variables
I. Using Global Variables
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
void f1() { x++; }
void f1() { x++; }
void f2() { x+=4; f1(); }
void f2() { x+=4; f1(); }
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
16
16
What Happens When We Use
What Happens When We Use
Inline Keyword?
Inline Keyword?
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
Inline
Inline void f1() { x++; }
void f1() { x++; }
Inline
Inline void f2() { x+=4; f1();}
void f2() { x+=4; f1();}
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
17
17
What Happens When We Use
What Happens When We Use
Inline Keyword?
Inline Keyword?
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
Inline
Inline void f1() { x++; }
void f1() { x++; }
Inline
Inline void f2() { x+=4; f1();}
void f2() { x+=4; f1();}
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
0
x
void main()
void main()
{
{
x+=4;
x+=4;
x++;
x++;
cout << x << endl;
cout << x << endl;
}
}
1
The inline keyword
instructs the compiler to
replace the function call
with the function body!
18
18
What Happens When We Use
What Happens When We Use
Inline Keyword?
Inline Keyword?
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
Inline
Inline void f1() { x++; }
void f1() { x++; }
Inline
Inline void f2() { x+=4; f1();}
void f2() { x+=4; f1();}
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
4
x
void main()
void main()
{
{
x+=4;
x+=4;
x++;
x++;
cout << x << endl;
cout << x << endl;
}
}
2
19
19
What Happens When We Use
What Happens When We Use
Inline Keyword?
Inline Keyword?
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
Inline
Inline void f1() { x++; }
void f1() { x++; }
Inline
Inline void f2() { x+=4; f1();}
void f2() { x+=4; f1();}
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
5
x
void main()
void main()
{
{
x+=4;
x+=4;
x++;
x++;
cout << x << endl;
cout << x << endl;
}
}
3
20
20
What Happens When We Use
What Happens When We Use
Inline Keyword?
Inline Keyword?
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
Inline
Inline void f1() { x++; }
void f1() { x++; }
Inline
Inline void f2() { x+=4; f1();}
void f2() { x+=4; f1();}
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
5
x
void main()
void main()
{
{
x+=4;
x+=4;
x++;
x++;
cout << x << endl;
cout << x << endl;
}
}
4
21
21
What Happens When We Use
What Happens When We Use
Inline Keyword?
Inline Keyword?
#include <iostream.h>
#include <iostream.h>
int x = 0;
int x = 0;
Inline
Inline void f1() { x++; }
void f1() { x++; }
Inline
Inline void f2() { x+=4; f1();}
void f2() { x+=4; f1();}
void main()
void main()
{
{
f2();
f2();
cout << x << endl;
cout << x << endl;
}
}
22
22
What is Bad About Using
What is Bad About Using
Global Vairables?
Global Vairables?
 Not safe!
Not safe!
 If two or more programmers are working together in a
If two or more programmers are working together in a
program, one of them may change the value stored in
program, one of them may change the value stored in
the global variable without telling the others who may
the global variable without telling the others who may
depend in their calculation on the old stored value!
depend in their calculation on the old stored value!
 Against The Principle of Information Hiding!
Against The Principle of Information Hiding!
 Exposing the global variables to all functions is
Exposing the global variables to all functions is
against the principle of information hiding since this
against the principle of information hiding since this
gives all functions the freedom to change the values
gives all functions the freedom to change the values
stored in the global variables at any time (unsafe!)
stored in the global variables at any time (unsafe!)
23
23
Local Variables
Local Variables
 Local variables are declared inside the function
Local variables are declared inside the function
body and exist as long as the function is running
body and exist as long as the function is running
and destroyed when the function exit
and destroyed when the function exit
 You have to initialize the local variable before
You have to initialize the local variable before
using it
using it
 If a function defines a local variable and there
If a function defines a local variable and there
was a global variable with the same name, the
was a global variable with the same name, the
function uses its local variable instead of using
function uses its local variable instead of using
the global variable
the global variable
24
24
Example of Defining and Using
Example of Defining and Using
Global and Local Variables
Global and Local Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
Void fun();
Void fun(); // function signature
// function signature
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
void fun()
void fun()
{
{
int x = 10;
int x = 10; // Local variable
// Local variable
cout << x << endl;
cout << x << endl;
}
}
25
25
Example of Defining and Using
Example of Defining and Using
Global and Local Variables
Global and Local Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
Void fun();
Void fun(); // function signature
// function signature
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
void fun()
void fun()
{
{
int x = 10;
int x = 10; // Local variable
// Local variable
cout << x << endl;
cout << x << endl;
}
}
x 0
Global variables are
automatically initialized to 0
26
26
Example of Defining and Using
Example of Defining and Using
Global and Local Variables
Global and Local Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
Void fun();
Void fun(); // function signature
// function signature
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
void fun()
void fun()
{
{
int x = 10;
int x = 10; // Local variable
// Local variable
cout << x << endl;
cout << x << endl;
}
}
x 0
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
1
27
27
Example of Defining and Using
Example of Defining and Using
Global and Local Variables
Global and Local Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
Void fun();
Void fun(); // function signature
// function signature
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
void fun()
void fun()
{
{
int x = 10;
int x = 10; // Local variable
// Local variable
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
2
void fun()
void fun()
{
{
int x = 10;
int x = 10;
cout << x << endl;
cout << x << endl;
}
}
x ????
3
28
28
Example of Defining and Using
Example of Defining and Using
Global and Local Variables
Global and Local Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
Void fun();
Void fun(); // function signature
// function signature
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
void fun()
void fun()
{
{
int x = 10;
int x = 10; // Local variable
// Local variable
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
2
void fun()
void fun()
{
{
int x = 10;
int x = 10;
cout << x << endl;
cout << x << endl;
}
}
x 10
3
29
29
Example of Defining and Using
Example of Defining and Using
Global and Local Variables
Global and Local Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
Void fun();
Void fun(); // function signature
// function signature
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
void fun()
void fun()
{
{
int x = 10;
int x = 10; // Local variable
// Local variable
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
2
void fun()
void fun()
{
{
int x = 10;
int x = 10;
cout << x << endl;
cout << x << endl;
}
}
x 10
4
30
30
Example of Defining and Using
Example of Defining and Using
Global and Local Variables
Global and Local Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
Void fun();
Void fun(); // function signature
// function signature
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
void fun()
void fun()
{
{
int x = 10;
int x = 10; // Local variable
// Local variable
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
2
void fun()
void fun()
{
{
int x = 10;
int x = 10;
cout << x << endl;
cout << x << endl;
}
}
x 10
5
31
31
Example of Defining and Using
Example of Defining and Using
Global and Local Variables
Global and Local Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
Void fun();
Void fun(); // function signature
// function signature
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
void fun()
void fun()
{
{
int x = 10;
int x = 10; // Local variable
// Local variable
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
6
32
32
Example of Defining and Using
Example of Defining and Using
Global and Local Variables
Global and Local Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
Void fun();
Void fun(); // function signature
// function signature
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
void fun()
void fun()
{
{
int x = 10;
int x = 10; // Local variable
// Local variable
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun();
fun();
cout << x << endl;
cout << x << endl;
}
}
7
33
33
II. Using Parameters
II. Using Parameters
 Function Parameters come in three
Function Parameters come in three
flavors:
flavors:
Value parameters
Value parameters – which copy the values of
– which copy the values of
the function arguments
the function arguments
Reference parameters
Reference parameters – which refer to the
– which refer to the
function arguments by other local names and
function arguments by other local names and
have the ability to change the values of the
have the ability to change the values of the
referenced arguments
referenced arguments
Constant reference parameters
Constant reference parameters – similar to
– similar to
the reference parameters but cannot change
the reference parameters but cannot change
the values of the referenced arguments
the values of the referenced arguments
34
34
Value Parameters
Value Parameters
 This is what we use to declare in the function signature or
This is what we use to declare in the function signature or
function header, e.g.
function header, e.g.
int max (int x, int y);
int max (int x, int y);
 Here, parameters x and y are value parameters
Here, parameters x and y are value parameters
 When you call the max function as
When you call the max function as max(4, 7)
max(4, 7), the values 4 and 7
, the values 4 and 7
are copied to x and y respectively
are copied to x and y respectively
 When you call the max function as
When you call the max function as max (a, b),
max (a, b), where a=40 and
where a=40 and
b=10, the values 40 and 10 are copied to x and y respectively
b=10, the values 40 and 10 are copied to x and y respectively
 When you call the max function as
When you call the max function as max( a+b, b/2),
max( a+b, b/2), the values 50
the values 50
and 5 are copies to x and y respectively
and 5 are copies to x and y respectively
 Once the value parameters accepted copies of the
Once the value parameters accepted copies of the
corresponding arguments data, they act as local
corresponding arguments data, they act as local
variables!
variables!
35
35
Example of Using Value
Example of Using Value
Parameters and Global Variables
Parameters and Global Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
void fun(int x)
void fun(int x)
{
{
cout << x << endl;
cout << x << endl;
x=x+5;
x=x+5;
}
}
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
x 0
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
1
36
36
Example of Using Value
Example of Using Value
Parameters and Global Variables
Parameters and Global Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
void fun(int x)
void fun(int x)
{
{
cout << x << endl;
cout << x << endl;
x=x+5;
x=x+5;
}
}
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
2
void fun(int x )
void fun(int x )
{
{
cout << x << endl;
cout << x << endl;
x=x+5;
x=x+5;
}
}
3
3
37
37
Example of Using Value
Example of Using Value
Parameters and Global Variables
Parameters and Global Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
void fun(int x)
void fun(int x)
{
{
cout << x << endl;
cout << x << endl;
x=x+5;
x=x+5;
}
}
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
2
void fun(int x )
void fun(int x )
{
{
cout << x << endl;
cout << x << endl;
x=x+5;
x=x+5;
}
}
3
4
8
38
38
Example of Using Value
Example of Using Value
Parameters and Global Variables
Parameters and Global Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
void fun(int x)
void fun(int x)
{
{
cout << x << endl;
cout << x << endl;
x=x+5;
x=x+5;
}
}
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
2
void fun(int x )
void fun(int x )
{
{
cout << x << endl;
cout << x << endl;
x=x+5;
x=x+5;
}
}
3
8
5
39
39
Example of Using Value
Example of Using Value
Parameters and Global Variables
Parameters and Global Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
void fun(int x)
void fun(int x)
{
{
cout << x << endl;
cout << x << endl;
x=x+5;
x=x+5;
}
}
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
6
40
40
Example of Using Value
Example of Using Value
Parameters and Global Variables
Parameters and Global Variables
#include <iostream.h>
#include <iostream.h>
int x;
int x; // Global variable
// Global variable
void fun(int x)
void fun(int x)
{
{
cout << x << endl;
cout << x << endl;
x=x+5;
x=x+5;
}
}
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
x 4
void main()
void main()
{
{
x = 4;
x = 4;
fun(x/2+1);
fun(x/2+1);
cout << x << endl;
cout << x << endl;
}
}
7
41
41
Reference Parameters
Reference Parameters
 As we saw in the last example, any changes in
As we saw in the last example, any changes in
the value parameters don’t affect the original
the value parameters don’t affect the original
function arguments
function arguments
 Sometimes, we want to change the values of the
Sometimes, we want to change the values of the
original function arguments or return with more
original function arguments or return with more
than one value from the function, in this case we
than one value from the function, in this case we
use reference parameters
use reference parameters
 A reference parameter is just another name to the
A reference parameter is just another name to the
original argument variable
original argument variable
 We define a reference parameter by adding the & in
We define a reference parameter by adding the & in
front of the parameter name, e.g.
front of the parameter name, e.g.
double update (double
double update (double &
& x);
x);
42
42
Example of Reference Parameters
Example of Reference Parameters
#include <iostream.h>
#include <iostream.h>
void fun(int &y)
void fun(int &y)
{
{
cout << y << endl;
cout << y << endl;
y=y+5;
y=y+5;
}
}
void main()
void main()
{
{
int x = 4;
int x = 4; // Local variable
// Local variable
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
void main()
void main()
{
{
int x = 4;
int x = 4;
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
1 x
? x
4
43
43
Example of Reference Parameters
Example of Reference Parameters
#include <iostream.h>
#include <iostream.h>
void fun(int &y)
void fun(int &y)
{
{
cout << y << endl;
cout << y << endl;
y=y+5;
y=y+5;
}
}
void main()
void main()
{
{
int x = 4; // Local variable
int x = 4; // Local variable
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
void main()
void main()
{
{
int x = 4;
int x = 4;
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
2
x
? x
4
void fun( int & y )
void fun( int & y )
{
{
cout<<y<<endl;
cout<<y<<endl;
y=y+5;
y=y+5;
}
}
3
44
44
Example of Reference Parameters
Example of Reference Parameters
#include <iostream.h>
#include <iostream.h>
void fun(int &y)
void fun(int &y)
{
{
cout << y << endl;
cout << y << endl;
y=y+5;
y=y+5;
}
}
void main()
void main()
{
{
int x = 4; // Local variable
int x = 4; // Local variable
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
void main()
void main()
{
{
int x = 4;
int x = 4;
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
2
x
? x
4
void fun( int & y )
void fun( int & y )
{
{
cout<<y<<endl;
cout<<y<<endl;
y=y+5;
y=y+5;
}
}
4 9
45
45
Example of Reference Parameters
Example of Reference Parameters
#include <iostream.h>
#include <iostream.h>
void fun(int &y)
void fun(int &y)
{
{
cout << y << endl;
cout << y << endl;
y=y+5;
y=y+5;
}
}
void main()
void main()
{
{
int x = 4; // Local variable
int x = 4; // Local variable
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
void main()
void main()
{
{
int x = 4;
int x = 4;
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
2
x
? x
9
void fun( int & y )
void fun( int & y )
{
{
cout<<y<<endl;
cout<<y<<endl;
y=y+5;
y=y+5;
}
}
5
46
46
Example of Reference Parameters
Example of Reference Parameters
#include <iostream.h>
#include <iostream.h>
void fun(int &y)
void fun(int &y)
{
{
cout << y << endl;
cout << y << endl;
y=y+5;
y=y+5;
}
}
void main()
void main()
{
{
int x = 4; // Local variable
int x = 4; // Local variable
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
void main()
void main()
{
{
int x = 4;
int x = 4;
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
6
x
? x
9
47
47
Example of Reference Parameters
Example of Reference Parameters
#include <iostream.h>
#include <iostream.h>
void fun(int &y)
void fun(int &y)
{
{
cout << y << endl;
cout << y << endl;
y=y+5;
y=y+5;
}
}
void main()
void main()
{
{
int x = 4; // Local variable
int x = 4; // Local variable
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
void main()
void main()
{
{
int x = 4;
int x = 4;
fun(x);
fun(x);
cout << x << endl;
cout << x << endl;
}
}
x
? x
9
7
48
48
Constant Reference Parameters
Constant Reference Parameters
 Constant reference parameters are used under
Constant reference parameters are used under
the following two conditions:
the following two conditions:
 The passed data are so big and you want to save
The passed data are so big and you want to save
time and computer memory
time and computer memory
 The passed data will not be changed or updated in
The passed data will not be changed or updated in
the function body
the function body
 For example
For example
void report (
void report (const
const string
string &
& prompt);
prompt);
 The only valid arguments accepted by reference
The only valid arguments accepted by reference
parameters and constant reference parameters
parameters and constant reference parameters
are variable names
are variable names
 It is a syntax error to pass constant values or
It is a syntax error to pass constant values or
expressions to the (const) reference parameters
expressions to the (const) reference parameters

More Related Content

PDF
how to reuse code
PPTX
Compiler Optimization Presentation
PDF
C++ L05-Functions
PPTX
functions
PPT
Functions in c++
PPTX
Part 3-functions1-120315220356-phpapp01
how to reuse code
Compiler Optimization Presentation
C++ L05-Functions
functions
Functions in c++
Part 3-functions1-120315220356-phpapp01

Similar to Isorerism in relative strength in inline functions in pine script.ppt (20)

PPTX
CP 04.pptx
PPTX
Go Programming Language (Golang)
PDF
C++ L03-Control Structure
PPTX
#OOP_D_ITS - 2nd - C++ Getting Started
PPT
443600107-1-Introduction-to education -C-ppt
PPT
443600107-1-Introduction-to-C-ppt (1).ppt
PPTX
OOPS PROGRAM for the clarity of functions in c++
PDF
Functional Programming Patterns (NDC London 2014)
PPT
C++ Functions.ppt
PPTX
Chapter 4
PPTX
From clever code to better code
DOCX
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PPTX
Learn c++ (functions) with nauman ur rehman
PPTX
Function recap
PPTX
Function recap
PPTX
Rust Intro
PPT
Practical basics on c++
PPTX
C++ Lambda and concurrency
PPT
Paradigmas de Linguagens de Programacao - Aula #4
PPTX
Lec 2.pptx programing errors \basic of programing
CP 04.pptx
Go Programming Language (Golang)
C++ L03-Control Structure
#OOP_D_ITS - 2nd - C++ Getting Started
443600107-1-Introduction-to education -C-ppt
443600107-1-Introduction-to-C-ppt (1).ppt
OOPS PROGRAM for the clarity of functions in c++
Functional Programming Patterns (NDC London 2014)
C++ Functions.ppt
Chapter 4
From clever code to better code
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
Learn c++ (functions) with nauman ur rehman
Function recap
Function recap
Rust Intro
Practical basics on c++
C++ Lambda and concurrency
Paradigmas de Linguagens de Programacao - Aula #4
Lec 2.pptx programing errors \basic of programing
Ad

Recently uploaded (20)

PPTX
Supervised vs unsupervised machine learning algorithms
PPTX
Computer network topology notes for revision
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
PPTX
SAP 2 completion done . PRESENTATION.pptx
PDF
[EN] Industrial Machine Downtime Prediction
PPTX
1_Introduction to advance data techniques.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Introduction to machine learning and Linear Models
PDF
Business Analytics and business intelligence.pdf
PDF
Mega Projects Data Mega Projects Data
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
PPTX
Database Infoormation System (DBIS).pptx
PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
Supervised vs unsupervised machine learning algorithms
Computer network topology notes for revision
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
mbdjdhjjodule 5-1 rhfhhfjtjjhafbrhfnfbbfnb
SAP 2 completion done . PRESENTATION.pptx
[EN] Industrial Machine Downtime Prediction
1_Introduction to advance data techniques.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
.pdf is not working space design for the following data for the following dat...
Introduction to machine learning and Linear Models
Business Analytics and business intelligence.pdf
Mega Projects Data Mega Projects Data
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Clinical guidelines as a resource for EBP(1).pdf
ALIMENTARY AND BILIARY CONDITIONS 3-1.pptx
Database Infoormation System (DBIS).pptx
climate analysis of Dhaka ,Banglades.pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
Reliability_Chapter_ presentation 1221.5784
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
Ad

Isorerism in relative strength in inline functions in pine script.ppt

  • 1. 1 1 Example #2 Example #2  Write a function to compute the distance Write a function to compute the distance between two points (x1, y1) and (x2, y2) between two points (x1, y1) and (x2, y2) Inline double distance (double x1, double y1, Inline double distance (double x1, double y1, double x2, double y2) double x2, double y2) { { return sqrt(pow(x1-x2,2)+pow(y1-y2,2)); return sqrt(pow(x1-x2,2)+pow(y1-y2,2)); } }
  • 2. 2 2 Example #3 Example #3  Write a function to compute n! Write a function to compute n! int factorial( int n) int factorial( int n) { { int product=1; int product=1; for (int i=1; i<=n; i++) product *= i; for (int i=1; i<=n; i++) product *= i; return product; return product; } }
  • 3. 3 3 Example #4 Example #4 Function Overloading Function Overloading  Write functions to return with the maximum number of Write functions to return with the maximum number of two numbers two numbers inline int max( int x, int y) inline int max( int x, int y) { { if (x>y) return x; else return y; if (x>y) return x; else return y; } } inline double max( double x, double y) inline double max( double x, double y) { { if (x>y) return x; else return y; if (x>y) return x; else return y; } } An overloaded function is a function that is defined more than once with different data types or different number of parameters
  • 4. 4 4 Sharing Data Among Sharing Data Among User-Defined Functions User-Defined Functions There are two ways to share data There are two ways to share data among different functions among different functions Using global variables (very bad practice!) Using global variables (very bad practice!) Passing data through function parameters Passing data through function parameters Value parameters Value parameters Reference parameters Reference parameters Constant reference parameters Constant reference parameters
  • 5. 5 5 C++ Variables C++ Variables  A variable is a place in memory that has A variable is a place in memory that has  A name or identifier (e.g. income, taxes, etc.) A name or identifier (e.g. income, taxes, etc.)  A data type (e.g. int, double, char, etc.) A data type (e.g. int, double, char, etc.)  A size (number of bytes) A size (number of bytes)  A scope (the part of the program code that can use it) A scope (the part of the program code that can use it)  Global variables – all functions can see it and using it Global variables – all functions can see it and using it  Local variables – only the function that declare local Local variables – only the function that declare local variables see and use these variables variables see and use these variables  A life time (the duration of its existence) A life time (the duration of its existence)  Global variables can live as long as the program is executed Global variables can live as long as the program is executed  Local variables are lived only when the functions that define Local variables are lived only when the functions that define these variables are executed these variables are executed
  • 6. 6 6 I. Using Global Variables I. Using Global Variables #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; void f1() { x++; } void f1() { x++; } void f2() { x+=4; f1(); } void f2() { x+=4; f1(); } void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } }
  • 7. 7 7 I. Using Global Variables I. Using Global Variables #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; void f1() { x++; } void f1() { x++; } void f2() { x+=4; f1(); } void f2() { x+=4; f1(); } void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } x 0
  • 8. 8 8 I. Using Global Variables I. Using Global Variables #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; void f1() { x++; } void f1() { x++; } void f2() { x+=4; f1(); } void f2() { x+=4; f1(); } void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } x 0 void main() void main() { { f2(); f2(); cout << x << endl ; cout << x << endl ; } } 1
  • 9. 9 9 I. Using Global Variables I. Using Global Variables #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; void f1() { x++; } void f1() { x++; } void f2() { x+=4; f1(); } void f2() { x+=4; f1(); } void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } x 0 void main() void main() { { f2(); f2(); cout << x << endl ; cout << x << endl ; } } 1 void f2() void f2() { { x += 4; x += 4; f1(); f1(); } } 2 4
  • 10. 10 10 4 5 I. Using Global Variables I. Using Global Variables #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; void f1() { x++; } void f1() { x++; } void f2() { x+=4; f1(); } void f2() { x+=4; f1(); } void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } x void main() void main() { { f2(); f2(); cout << x << endl ; cout << x << endl ; } } 1 void f2() void f2() { { x += 4; x += 4; f1(); f1(); } } 3 void f1() void f1() { { x++; x++; } } 4
  • 11. 11 11 4 5 I. Using Global Variables I. Using Global Variables #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; void f1() { x++; } void f1() { x++; } void f2() { x+=4; f1(); } void f2() { x+=4; f1(); } void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } x void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } 1 void f2() void f2() { { x += 4; x += 4; f1(); f1(); } } 3 void f1() void f1() { { x++; x++; } } 5
  • 12. 12 12 4 5 I. Using Global Variables I. Using Global Variables #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; void f1() { x++; } void f1() { x++; } void f2() { x+=4; f1(); } void f2() { x+=4; f1(); } void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } x void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } 1 void f2() void f2() { { x += 4; x += 4; f1(); f1(); } } 6
  • 13. 13 13 4 5 I. Using Global Variables I. Using Global Variables #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; void f1() { x++; } void f1() { x++; } void f2() { x+=4; f1(); } void f2() { x+=4; f1(); } void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } x void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } 7
  • 14. 14 14 4 5 I. Using Global Variables I. Using Global Variables #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; void f1() { x++; } void f1() { x++; } void f2() { x+=4; f1(); } void f2() { x+=4; f1(); } void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } x void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } 8
  • 15. 15 15 I. Using Global Variables I. Using Global Variables #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; void f1() { x++; } void f1() { x++; } void f2() { x+=4; f1(); } void f2() { x+=4; f1(); } void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } }
  • 16. 16 16 What Happens When We Use What Happens When We Use Inline Keyword? Inline Keyword? #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; Inline Inline void f1() { x++; } void f1() { x++; } Inline Inline void f2() { x+=4; f1();} void f2() { x+=4; f1();} void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } }
  • 17. 17 17 What Happens When We Use What Happens When We Use Inline Keyword? Inline Keyword? #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; Inline Inline void f1() { x++; } void f1() { x++; } Inline Inline void f2() { x+=4; f1();} void f2() { x+=4; f1();} void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } 0 x void main() void main() { { x+=4; x+=4; x++; x++; cout << x << endl; cout << x << endl; } } 1 The inline keyword instructs the compiler to replace the function call with the function body!
  • 18. 18 18 What Happens When We Use What Happens When We Use Inline Keyword? Inline Keyword? #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; Inline Inline void f1() { x++; } void f1() { x++; } Inline Inline void f2() { x+=4; f1();} void f2() { x+=4; f1();} void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } 4 x void main() void main() { { x+=4; x+=4; x++; x++; cout << x << endl; cout << x << endl; } } 2
  • 19. 19 19 What Happens When We Use What Happens When We Use Inline Keyword? Inline Keyword? #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; Inline Inline void f1() { x++; } void f1() { x++; } Inline Inline void f2() { x+=4; f1();} void f2() { x+=4; f1();} void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } 5 x void main() void main() { { x+=4; x+=4; x++; x++; cout << x << endl; cout << x << endl; } } 3
  • 20. 20 20 What Happens When We Use What Happens When We Use Inline Keyword? Inline Keyword? #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; Inline Inline void f1() { x++; } void f1() { x++; } Inline Inline void f2() { x+=4; f1();} void f2() { x+=4; f1();} void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } } 5 x void main() void main() { { x+=4; x+=4; x++; x++; cout << x << endl; cout << x << endl; } } 4
  • 21. 21 21 What Happens When We Use What Happens When We Use Inline Keyword? Inline Keyword? #include <iostream.h> #include <iostream.h> int x = 0; int x = 0; Inline Inline void f1() { x++; } void f1() { x++; } Inline Inline void f2() { x+=4; f1();} void f2() { x+=4; f1();} void main() void main() { { f2(); f2(); cout << x << endl; cout << x << endl; } }
  • 22. 22 22 What is Bad About Using What is Bad About Using Global Vairables? Global Vairables?  Not safe! Not safe!  If two or more programmers are working together in a If two or more programmers are working together in a program, one of them may change the value stored in program, one of them may change the value stored in the global variable without telling the others who may the global variable without telling the others who may depend in their calculation on the old stored value! depend in their calculation on the old stored value!  Against The Principle of Information Hiding! Against The Principle of Information Hiding!  Exposing the global variables to all functions is Exposing the global variables to all functions is against the principle of information hiding since this against the principle of information hiding since this gives all functions the freedom to change the values gives all functions the freedom to change the values stored in the global variables at any time (unsafe!) stored in the global variables at any time (unsafe!)
  • 23. 23 23 Local Variables Local Variables  Local variables are declared inside the function Local variables are declared inside the function body and exist as long as the function is running body and exist as long as the function is running and destroyed when the function exit and destroyed when the function exit  You have to initialize the local variable before You have to initialize the local variable before using it using it  If a function defines a local variable and there If a function defines a local variable and there was a global variable with the same name, the was a global variable with the same name, the function uses its local variable instead of using function uses its local variable instead of using the global variable the global variable
  • 24. 24 24 Example of Defining and Using Example of Defining and Using Global and Local Variables Global and Local Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable Void fun(); Void fun(); // function signature // function signature void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } void fun() void fun() { { int x = 10; int x = 10; // Local variable // Local variable cout << x << endl; cout << x << endl; } }
  • 25. 25 25 Example of Defining and Using Example of Defining and Using Global and Local Variables Global and Local Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable Void fun(); Void fun(); // function signature // function signature void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } void fun() void fun() { { int x = 10; int x = 10; // Local variable // Local variable cout << x << endl; cout << x << endl; } } x 0 Global variables are automatically initialized to 0
  • 26. 26 26 Example of Defining and Using Example of Defining and Using Global and Local Variables Global and Local Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable Void fun(); Void fun(); // function signature // function signature void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } void fun() void fun() { { int x = 10; int x = 10; // Local variable // Local variable cout << x << endl; cout << x << endl; } } x 0 void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } 1
  • 27. 27 27 Example of Defining and Using Example of Defining and Using Global and Local Variables Global and Local Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable Void fun(); Void fun(); // function signature // function signature void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } void fun() void fun() { { int x = 10; int x = 10; // Local variable // Local variable cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } 2 void fun() void fun() { { int x = 10; int x = 10; cout << x << endl; cout << x << endl; } } x ???? 3
  • 28. 28 28 Example of Defining and Using Example of Defining and Using Global and Local Variables Global and Local Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable Void fun(); Void fun(); // function signature // function signature void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } void fun() void fun() { { int x = 10; int x = 10; // Local variable // Local variable cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } 2 void fun() void fun() { { int x = 10; int x = 10; cout << x << endl; cout << x << endl; } } x 10 3
  • 29. 29 29 Example of Defining and Using Example of Defining and Using Global and Local Variables Global and Local Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable Void fun(); Void fun(); // function signature // function signature void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } void fun() void fun() { { int x = 10; int x = 10; // Local variable // Local variable cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } 2 void fun() void fun() { { int x = 10; int x = 10; cout << x << endl; cout << x << endl; } } x 10 4
  • 30. 30 30 Example of Defining and Using Example of Defining and Using Global and Local Variables Global and Local Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable Void fun(); Void fun(); // function signature // function signature void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } void fun() void fun() { { int x = 10; int x = 10; // Local variable // Local variable cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } 2 void fun() void fun() { { int x = 10; int x = 10; cout << x << endl; cout << x << endl; } } x 10 5
  • 31. 31 31 Example of Defining and Using Example of Defining and Using Global and Local Variables Global and Local Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable Void fun(); Void fun(); // function signature // function signature void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } void fun() void fun() { { int x = 10; int x = 10; // Local variable // Local variable cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } 6
  • 32. 32 32 Example of Defining and Using Example of Defining and Using Global and Local Variables Global and Local Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable Void fun(); Void fun(); // function signature // function signature void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } void fun() void fun() { { int x = 10; int x = 10; // Local variable // Local variable cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(); fun(); cout << x << endl; cout << x << endl; } } 7
  • 33. 33 33 II. Using Parameters II. Using Parameters  Function Parameters come in three Function Parameters come in three flavors: flavors: Value parameters Value parameters – which copy the values of – which copy the values of the function arguments the function arguments Reference parameters Reference parameters – which refer to the – which refer to the function arguments by other local names and function arguments by other local names and have the ability to change the values of the have the ability to change the values of the referenced arguments referenced arguments Constant reference parameters Constant reference parameters – similar to – similar to the reference parameters but cannot change the reference parameters but cannot change the values of the referenced arguments the values of the referenced arguments
  • 34. 34 34 Value Parameters Value Parameters  This is what we use to declare in the function signature or This is what we use to declare in the function signature or function header, e.g. function header, e.g. int max (int x, int y); int max (int x, int y);  Here, parameters x and y are value parameters Here, parameters x and y are value parameters  When you call the max function as When you call the max function as max(4, 7) max(4, 7), the values 4 and 7 , the values 4 and 7 are copied to x and y respectively are copied to x and y respectively  When you call the max function as When you call the max function as max (a, b), max (a, b), where a=40 and where a=40 and b=10, the values 40 and 10 are copied to x and y respectively b=10, the values 40 and 10 are copied to x and y respectively  When you call the max function as When you call the max function as max( a+b, b/2), max( a+b, b/2), the values 50 the values 50 and 5 are copies to x and y respectively and 5 are copies to x and y respectively  Once the value parameters accepted copies of the Once the value parameters accepted copies of the corresponding arguments data, they act as local corresponding arguments data, they act as local variables! variables!
  • 35. 35 35 Example of Using Value Example of Using Value Parameters and Global Variables Parameters and Global Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable void fun(int x) void fun(int x) { { cout << x << endl; cout << x << endl; x=x+5; x=x+5; } } void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } x 0 void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } 1
  • 36. 36 36 Example of Using Value Example of Using Value Parameters and Global Variables Parameters and Global Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable void fun(int x) void fun(int x) { { cout << x << endl; cout << x << endl; x=x+5; x=x+5; } } void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } 2 void fun(int x ) void fun(int x ) { { cout << x << endl; cout << x << endl; x=x+5; x=x+5; } } 3 3
  • 37. 37 37 Example of Using Value Example of Using Value Parameters and Global Variables Parameters and Global Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable void fun(int x) void fun(int x) { { cout << x << endl; cout << x << endl; x=x+5; x=x+5; } } void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } 2 void fun(int x ) void fun(int x ) { { cout << x << endl; cout << x << endl; x=x+5; x=x+5; } } 3 4 8
  • 38. 38 38 Example of Using Value Example of Using Value Parameters and Global Variables Parameters and Global Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable void fun(int x) void fun(int x) { { cout << x << endl; cout << x << endl; x=x+5; x=x+5; } } void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } 2 void fun(int x ) void fun(int x ) { { cout << x << endl; cout << x << endl; x=x+5; x=x+5; } } 3 8 5
  • 39. 39 39 Example of Using Value Example of Using Value Parameters and Global Variables Parameters and Global Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable void fun(int x) void fun(int x) { { cout << x << endl; cout << x << endl; x=x+5; x=x+5; } } void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } 6
  • 40. 40 40 Example of Using Value Example of Using Value Parameters and Global Variables Parameters and Global Variables #include <iostream.h> #include <iostream.h> int x; int x; // Global variable // Global variable void fun(int x) void fun(int x) { { cout << x << endl; cout << x << endl; x=x+5; x=x+5; } } void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } x 4 void main() void main() { { x = 4; x = 4; fun(x/2+1); fun(x/2+1); cout << x << endl; cout << x << endl; } } 7
  • 41. 41 41 Reference Parameters Reference Parameters  As we saw in the last example, any changes in As we saw in the last example, any changes in the value parameters don’t affect the original the value parameters don’t affect the original function arguments function arguments  Sometimes, we want to change the values of the Sometimes, we want to change the values of the original function arguments or return with more original function arguments or return with more than one value from the function, in this case we than one value from the function, in this case we use reference parameters use reference parameters  A reference parameter is just another name to the A reference parameter is just another name to the original argument variable original argument variable  We define a reference parameter by adding the & in We define a reference parameter by adding the & in front of the parameter name, e.g. front of the parameter name, e.g. double update (double double update (double & & x); x);
  • 42. 42 42 Example of Reference Parameters Example of Reference Parameters #include <iostream.h> #include <iostream.h> void fun(int &y) void fun(int &y) { { cout << y << endl; cout << y << endl; y=y+5; y=y+5; } } void main() void main() { { int x = 4; int x = 4; // Local variable // Local variable fun(x); fun(x); cout << x << endl; cout << x << endl; } } void main() void main() { { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl; } } 1 x ? x 4
  • 43. 43 43 Example of Reference Parameters Example of Reference Parameters #include <iostream.h> #include <iostream.h> void fun(int &y) void fun(int &y) { { cout << y << endl; cout << y << endl; y=y+5; y=y+5; } } void main() void main() { { int x = 4; // Local variable int x = 4; // Local variable fun(x); fun(x); cout << x << endl; cout << x << endl; } } void main() void main() { { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl; } } 2 x ? x 4 void fun( int & y ) void fun( int & y ) { { cout<<y<<endl; cout<<y<<endl; y=y+5; y=y+5; } } 3
  • 44. 44 44 Example of Reference Parameters Example of Reference Parameters #include <iostream.h> #include <iostream.h> void fun(int &y) void fun(int &y) { { cout << y << endl; cout << y << endl; y=y+5; y=y+5; } } void main() void main() { { int x = 4; // Local variable int x = 4; // Local variable fun(x); fun(x); cout << x << endl; cout << x << endl; } } void main() void main() { { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl; } } 2 x ? x 4 void fun( int & y ) void fun( int & y ) { { cout<<y<<endl; cout<<y<<endl; y=y+5; y=y+5; } } 4 9
  • 45. 45 45 Example of Reference Parameters Example of Reference Parameters #include <iostream.h> #include <iostream.h> void fun(int &y) void fun(int &y) { { cout << y << endl; cout << y << endl; y=y+5; y=y+5; } } void main() void main() { { int x = 4; // Local variable int x = 4; // Local variable fun(x); fun(x); cout << x << endl; cout << x << endl; } } void main() void main() { { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl; } } 2 x ? x 9 void fun( int & y ) void fun( int & y ) { { cout<<y<<endl; cout<<y<<endl; y=y+5; y=y+5; } } 5
  • 46. 46 46 Example of Reference Parameters Example of Reference Parameters #include <iostream.h> #include <iostream.h> void fun(int &y) void fun(int &y) { { cout << y << endl; cout << y << endl; y=y+5; y=y+5; } } void main() void main() { { int x = 4; // Local variable int x = 4; // Local variable fun(x); fun(x); cout << x << endl; cout << x << endl; } } void main() void main() { { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl; } } 6 x ? x 9
  • 47. 47 47 Example of Reference Parameters Example of Reference Parameters #include <iostream.h> #include <iostream.h> void fun(int &y) void fun(int &y) { { cout << y << endl; cout << y << endl; y=y+5; y=y+5; } } void main() void main() { { int x = 4; // Local variable int x = 4; // Local variable fun(x); fun(x); cout << x << endl; cout << x << endl; } } void main() void main() { { int x = 4; int x = 4; fun(x); fun(x); cout << x << endl; cout << x << endl; } } x ? x 9 7
  • 48. 48 48 Constant Reference Parameters Constant Reference Parameters  Constant reference parameters are used under Constant reference parameters are used under the following two conditions: the following two conditions:  The passed data are so big and you want to save The passed data are so big and you want to save time and computer memory time and computer memory  The passed data will not be changed or updated in The passed data will not be changed or updated in the function body the function body  For example For example void report ( void report (const const string string & & prompt); prompt);  The only valid arguments accepted by reference The only valid arguments accepted by reference parameters and constant reference parameters parameters and constant reference parameters are variable names are variable names  It is a syntax error to pass constant values or It is a syntax error to pass constant values or expressions to the (const) reference parameters expressions to the (const) reference parameters