Session 8 Functions
Session 8 Functions
void callByReference(int x)
{ x = 5; } The value of x is 3
int main()
{
int x = 3; //change does affect function call
callByReference( x );
cout << "The value of x is " << x;
return 0; }
#include <iostream> Call by reference
using namespace std;
void swap(int &, int &);
int main()
{
int a=10,b=20;
swap(a,b);
cout<<a<<" "<<b;
Answer
return 0;
}
20 10
void swap(int &c, int &d)
{
int t;
t=c;
c=d;
Call by Reference
#include <iostream>
using namespace std;
void callByReference(int& x)
{ x = 5; } The value of x is 5,
int main()
{
int x = 3; //change does not affect function call
callByReference( x );
cout << "The value of x is " << x;
return 0;
Variable Scope and Lifetime
• Thus far, variables have been defined on top
of the main function (Global).
• In programs where only main is the function,
the variables can be accessed throughout the
entire program.
• Once we begin dividing up codes into separate
functions, there arise the issue of variable
scope and lifetime.
Variable Scope and Lifetime Cont.
• A global variable have scope through out a
program and lifetime did not end until the
entire program ends.
• A static variable has scope of local variable but
lifetime of a global variable.
• A static local variable is declared with the
keyword static attached and usually with a
starting value
Local Variable
#include <iostream> else
using namespace std; printMessage();
void printMessage();//prototype
cout<<endl;
int main()
{ }while (choice != 'Q');
char choice; Keeps say 1 time
do return 0;
{ }
cout<<"enter Q to quit, any other void printMessage()
character to continue: ";
{
cin>> choice;
int times = 0;//local variable
if (choice == 'Q' ) times++;
{ cout<< "This function called
cout<<"input stopped"<<endl; "<<times<< " times"<<endl;
Local Variable
• The variable times is local to printMessage
function since it is declared in that function.
• Being a local variable each time the
printMessage function is called, the variable
times is created
• Also each time the printMessage function
ends, the variable times is destroyed.
• To make a variable persistent between
function calls
1. Keep the variable local but make it static.
Global Variable
#include <iostream> {
using namespace std; cout<<"input stopped"<<endl;
void printMessage();//prototype
}
int times;//Global Variable else
printMessage();
int main() cout<<endl;
{
char choice; }while (choice != 'Q');
do
Same old results
{ return 0;
times = 0; }
cout<<"enter Q to quit, any other void printMessage()
character to continue: "; {
cin>> choice; times++;
cout<< "This function called
if (choice == 'Q' ) "<<times<< " times"<<endl;
Static local Variable
#include <iostream> else
using namespace std; printMessage();
void printMessage();
cout<<endl;
int main() }while (choice != 'Q');
{ return 0;
char choice;
}
do
{ void printMessage()
cout<<"enter Q to quit, any other {
character to continue: ";
static int times =0;
cin>> choice;
times++;
if (choice == 'Q' ) cout<< "This function called
{ "<<times<< " times"<<endl;
cout<<"input stopped"<<endl;
} Result will now change
Stopping the program after 3 inputs
#include <iostream> if (choice == 'Q' )
using namespace std; {
void printMessage();
cout<<"input stopped"<<endl;
int main()
}
{ else
char choice; printMessage();
int counter=1; cout<<endl;
do }while (choice != 'Q');
{
return 0;
cout<<"enter Q to quit, any other character
to continue: "; }
cin>> choice; void printMessage()
if (counter == 3)//counting {
{ static int times;
cout<<"you have entered the value 3 times++;
times"<<endl;
break;
cout<< "This function called
}
"<<times<< " times"<<endl;
}
Sending message to a function
}
#include<iostream>
using namespace std;
Example 2