SlideShare a Scribd company logo
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2010, 11, 12, 13, 14 
C++ 
Programming Language 
L04-FUNCTIONS
Functions 
•Why function? 
•functions procedures in c++ 
•What’s the function prototype (declaration) definition? 
•Function signature (2010 exam Question) 
–What’s it? 
–It’s just the name of the function with its parameters 
•No return type!!!! (Return type is not considered as part of the function’s signature!, guess why?) 
intfoo(int);// function prototype 
foo(int) // function signature
Functions 
#include<iostream> 
usingnamespace::std; 
intfoo(int);// function prototype 
intmain() 
{ 
return0; 
} 
#include<iostream> 
usingnamespace::std; 
foo(int); // function prototype 
intmain() 
{ 
return0; 
} 
Compiler error, missing return type
Functions 
•The following are the same 
#include<iostream> 
usingnamespace::std; 
intMult( intx ) // note there's no; when we 
// write the definition here 
{ 
x = x * 2; 
returnx; 
} 
void main() 
{ 
} 
#include<iostream> 
usingnamespace::std; 
intMult( intx ); 
void main() 
{} 
intMult( intx ) 
{ 
x = x * 2; 
returnx; 
}
Functions 
•The following are the same 
#include<iostream> 
usingnamespace::std; 
intMult( int x ) 
{ 
x = x * 2; 
returnx; 
} 
void main() 
{ 
} 
#include<iostream> 
usingnamespace::std; 
intMult(int); // we didn’t write the x 
// pirmeted when prototype only 
void main() 
{} 
intMult( intx ) 
{ 
x = x * 2; 
returnx; 
}
Functions 
#include<iostream> 
usingnamespace::std; 
intMult(int); 
void main() 
{ 
Mult (3); 
} 
intMult( intx ) 
{ 
x = x * 2; 
returnx; 
} 
#include<iostream> 
usingnamespace::std; 
intMult(int); 
void main() 
{ 
cout << Mult(3); 
} 
intMult( intx ) 
{ 
x = x * 2; 
returnx; 
} 
6
Functions 
#include<iostream> 
usingnamespace::std; 
intMult(int); 
void main() 
{ 
Mult(3); 
} 
intMult( intx ) 
{ 
x = x * 2; 
cout << x; 
return0; 
} 
#include<iostream> 
usingnamespace::std; 
intMult(int); 
void main() 
{ 
int c = 3; 
Mult(c); 
} 
intMult( intx ) 
{ 
x = x * 2; 
cout << x; 
return0; 
} 
6 
6
Functions 
#include<iostream> 
usingnamespace::std; 
intMult(int); 
void main() 
{ 
int x = 3; 
Mult(x); 
} 
intMult( intx ) 
{ 
x = x * 2; 
cout << x; 
return0; 
} 
#include<iostream> 
usingnamespace::std; 
intMult(int); 
void main() 
{ 
int x = 3; 
Mult(x); 
cout << x; 
} 
intMult( intx ) 
{ 
x = x * 2; 
cout << x; 
return0; 
} 
6 
6
Functions 
#include<iostream> 
usingnamespace::std; 
intMult(int); 
void main() 
{ 
int x = 3; 
Mult(x); 
cout << endl; 
cout << x; 
} 
intMult( intx ) 
{ 
x = x * 2; 
cout << x; 
return0; 
} 
#include<iostream> 
usingnamespace::std; 
intMult(int); 
void main() 
{ 
int x = 3; 
Mult(x); 
cout << x; 
} 
intMult( intx ) 
{ 
x = x * 2; 
cout << x << endl; 
return0; 
} 
6 
3 
6 
3
Functions 
#include<iostream> 
usingnamespace::std; 
intMult(int); 
void main() 
{ 
int x = 3; 
cout << Mult(x) << endl; 
cout << x; 
} 
intMult( intx ) 
{ 
x = x * 2; 
returnx; 
} 
#include<iostream> 
usingnamespace::std; 
void Mult(void); 
void main() 
{ 
int x = 3; 
Mult() << endl; 
cout << x; 
} 
voidMult( void ) 
{ 
int x = 3; 
cout << x * 2 << endl; 
} 
6 
3 
Compiler error
Functions 
#include<iostream> 
usingnamespace::std; 
void Mult(void); 
void main() 
{ 
int c = 3; 
Mult() << endl; 
cout << c; 
} 
voidMult( void ) 
{ 
int x = 3; 
cout << x * 2 << endl; 
} 
#include<iostream> 
usingnamespace::std; 
void Mult(void); 
void main() 
{ 
Mult() << endl; 
} 
voidMult( void ) 
{ 
int x = 3; 
x = x * 2; 
return0; 
} 
Compiler error 
Compiler error, return 0; with void function
Functions 
#include<iostream> 
usingnamespace::std; 
void Mult(void); 
void main() 
{ 
cout << Mult() << endl; 
} 
voidMult() 
{ 
int x = 3; 
x = x * 2; 
} 
#include<iostream> 
usingnamespace::std; 
void Mult(void); 
void main(void) 
{ 
Mult(); 
} 
voidMult(void) 
{ 
int x = 3; 
x = x * 2; 
} 
Compiler error, cout with void return funtion 
At last everything’s working
Functions 
#include<iostream> 
usingnamespace::std; 
void Mult(void); 
void main(void) 
{ 
Mult(); 
} 
voidMult(void) 
{ 
int x = 3; 
x = x * 2; 
} 
#include<iostream> 
usingnamespace::std; 
void Mult(); 
void main() 
{ 
Mult(); 
} 
voidMult() 
{ 
int x = 3; 
x = x * 2; 
} 
Compile and run 
Compile and run. In the parameter list, voidor empty is the same
Functions 
#include<iostream> 
usingnamespace::std; 
voidMult(void); 
voidmain() 
{ 
Mult(); 
} 
Mult() 
{ 
int x = 3; 
x = x * 2; 
} 
#include<iostream> 
usingnamespace::std; 
Mult(void); 
voidmain() 
{ 
Mult(); 
} 
void Mult() 
{ 
int x = 3; 
x = x * 2; 
} 
Compiler error, missing return type 
Compiler error, missing return type
Functions 
#include<iostream> 
usingnamespace::std; 
void Mult(void); 
void main(void) 
{ 
Mult(); 
} 
int Mult(void) 
{ 
int x = 3; 
x = x * 2; 
returnx; 
} 
#include<iostream> 
usingnamespace::std; 
int Mult(void); 
void main(void) 
{ 
Mult(); 
} 
void Mult(void) 
{ 
int x = 3; 
x = x * 2; 
} 
Compiler error, return type differs 
Compiler error, return type differs
Functions 
#include<iostream> 
usingnamespace::std; 
int Mult(void); 
void main(void) 
{ 
cout << Mult() << endl; 
} 
int Mult(void) 
{ 
int x = 3; 
x = x * 2; 
returnx; 
} 
#include<iostream> 
usingnamespace::std; 
int Mult(void); 
void main(void) 
{ 
cout << Mult() << endl; 
} 
int Mult(void) 
{ 
int x = 3; 
returnx * 2; 
} 
6 
6
Functions 
#include<iostream> 
usingnamespace::std; 
int Mult(void); 
void main(void) 
{ 
cout << Mult() << endl; 
} 
int Mult(void) 
{ 
int x = 3; 
returnx * 2; cout << x << end; 
} 
#include<iostream> 
usingnamespace::std; 
int Mult(int x); 
void main(void) 
{ 
cout << Mult() << endl; 
} 
int Mult(int x) 
{ 
returnx * 2; 
} 
6 
Compiler error, no variable passed to function 
Nothing got excuted after the return statement
Functions 
#include<iostream> 
usingnamespace::std; 
int Mult(int x); 
void main(void) 
{ 
cout << Mult(3) << endl; 
} 
int Mult(int x) 
{ 
returnx * 2; 
} 
#include<iostream> 
usingnamespace::std; 
int Mult(int x); 
void main(void) 
{ 
cout << Mult(3) << endl; 
} 
int Mult(int x) 
{ 
intx = 2; 
returnx * 2; 
} 
0 
Compiler error, redefinition of variable x
Functions 
#include<iostream> 
usingnamespace::std; 
intMult(int, int); 
voidmain(void) 
{ 
inti1, i2; 
cout << Mult(i1,i2) << endl; 
} 
intMult(intx, inty) 
{ 
returnx * 2; 
} 
#include<iostream> 
usingnamespace::std; 
intMult(int, int); 
voidmain(void) 
{ 
inti1, i2; 
cout << Mult(i1,i2) << endl; 
} 
intMult(intx, y) 
{ 
returnx * 2; 
} 
6 
Compiler error, missing type for y
Functions 
#include<iostream> 
usingnamespace::std; 
intMult(int, int); 
voidmain(void) 
{ 
inti1, i2; 
cout << Mult(i1,i2) << endl; 
} 
intMult(intx, inty) 
{ 
if(x = 2 ) 
{ 
return3; 
} 
else 
{ 
returnx * 2; 
} 
} 
#include<iostream> 
usingnamespace::std; 
intMult(int, int); 
voidmain(void) 
{ 
inti1, i2; 
cout << Mult(i1,i2) << endl; 
} 
intMult(intx, inty) 
{ 
int MeMe (int z) 
{ 
return 0; 
} 
} 
3 
Compiler error, can’t define a function inside another one
Functions 
#include<iostream> 
usingnamespace::std; 
intMult(int, int); 
voidmain(void) 
{ 
floati1= 3.4, i2; 
cout << Mult(i1,i2) << endl; 
} 
intMult(intx, inty) 
{ 
return2*x; 
} 
#include<iostream> 
usingnamespace::std; 
intMult(int); 
voidmain(void) 
{ 
for(inti = 0; i<=10; i++) 
{ 
cout << Mult(i) << "-"; 
} 
cout << “hehe” << endl; 
cout<< endl; 
} 
intMult(intx ) 
{ 
return2*x; 
} 
6 
0-2-4-6-8-10-12-14-16-18-20-hehe 
Press any key to continue
Functions 
#include<iostream> 
usingnamespace::std; 
voidCrazy1(); 
voidCrazy2(int); 
voidmain(void) 
{Crazy1(); } 
voidCrazy1() 
{ 
intx = 3; 
Crazy2(x); 
cout << x; 
} 
voidCrazy2(intx ) 
{ 
x+=6; 
cout << x << endl; 
} 
9 
3
Headers
Headers 
•The Concept of headers
Headers File 
•What’s a header? 
–Moving functions out side the main program 
•Logical groups 
–Stored in their own files 
•How to do it? 
–By Moving 
•function declarations 
–Into headers files 
»(.h) files // header files 
•function definitions 
–Into source files 
»(.cpp) files // source files 
–Note: function definitions can’t be split up into multiple files!
Pre-defined Functions
cmathand sqrt 
#include<iostream> 
#include<cmath> 
usingnamespace::std; 
voidmain(void) 
{ 
cout << sqrt(100.0) << endl; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
cout << sqrt(100.0) << endl; 
} 
10 
Compiler error identifier not found 
•To use it 
–Include <cmath>
cmathand sqrt 
#include<iostream> 
#include<cmath> 
usingnamespace::std; 
voidmain(void) 
{ 
cout << sqrt(100) << endl; 
} 
#include<iostream> 
#include<cmath> 
usingnamespace::std; 
voidmain(void) 
{ 
cout << sqrt(sqrt(100.0)) << endl; 
} 
Compiler error, can’t use integer “no overloaded function” 
3.16228
cmathand sqrt 
#include<iostream> 
#include<cmath> 
usingnamespace::std; 
voidmain(void) 
{ 
intx; 
cout << sqrt(6*x) << endl; 
} 
#include<iostream> 
#include<cmath> 
usingnamespace::std; 
voidmain(void) 
{ 
doublex = 3; 
cout << sqrt(3*x) << endl; 
} 
Compiler error, can’t use integer “no overloaded function” 
3
cmathand sqrt 
#include<iostream> 
#include<cmath> 
usingnamespace::std; 
voidmain(void) 
{ 
float x = 3; 
cout << sqrt(3*x) << endl; 
} 
3
rand()
rand() 
•Random number 
–rand() 
–Needs to include <cstdlib> 
•Generates unsigned integers 
–Between 
»0 (Zero :D) 
»Rand_Maxnumber (usually 32767)
rand() 
#include<iostream> 
#include <cstdlib> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 3; 
x = rand(); 
cout << x << endl; 
} 
41 
Now, when compiling many times 
Every time we have the same random value! 
41 
41 
41
rand() 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 3; 
x = rand(); 
cout << x << endl; 
} 
#include<iostream> 
#include <cstdlib> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 3; 
x = rand(); 
cout << x << endl; 
cout << x << endl; 
} 
54 
156 
156 
Works without <cstlib>
rand() 
#include<iostream> 
#include <cstdlib> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 3; 
x = rand(); 
cout << x << endl; 
x = rand(); 
cout << x << endl; 
} 
156 
12356
srand()
srand() 
•srand() 
–#include <cstdlib> 
–Give a starting value for the rand()function 
–Usually used once in program
srand() 
#include<iostream> 
#include<ctime> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 3; 
srand (time(0)); 
cout << x << endl; 
} 
#include<iostream> 
#include<ctime> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 3; 
srand (time()); 
cout << x << endl; 
} 
3 
Compiler error, need value for time()
What happened when compiling many times? 
srand() 
#include<iostream> 
#include<ctime> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 3; 
srand (time(0)); 
x = rand (); 
cout << x << endl; 
} 
456 
Now, when compiling many times. Every time we have a new differentvalue 
12345 
189 
What happened when compiling many times? 
#include<iostream> 
#include<ctime> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 3; 
srand (5); 
x = rand (); 
cout << x << endl; 
} 
48 
Not an appropriate srand() paramater leads to insufficient using of srand()!!! 
48 
48
srand() with clock() 
#include<iostream> 
#include<ctime> 
usingnamespace::std; 
voidmain(void) 
{ 
srand(clock()); 
// clock is from <ctime> 
// no needs to value for clock () 
for(inti = 0; i < 10; i++) 
cout << rand() << endl; 
} 
714 
4415 
16337 
2807 
14052 
5430 
30050 
16163 
20477 
3146 
Press any key to continue
rand() 
•Scaling & shifting 
–% 
•x % y // returns a value between 0 to y-1 
•let’s see an example 
#include<iostream> 
#include<cstdlib> 
usingnamespace::std; 
voidmain(void) 
{ 
inti; 
i = rand() % 6 + 1; 
// rand() % 6: number between 0 to 5 
// +1: makes a shift of the range we have 
// 0 to 5 becomes 1 to 6 
cout << i << endl; 
// here will print a number absolutely between 1 to 6 
} 
6 
Or any other number between 1 and 6
Variables and Storage
Variables and Storage 
•Name, type, size, values 
•Storage class 
–Period variable living in memory 
•Linkage 
–Multiple-file processing, which files can use it. 
•Scope 
–Variable can be referenced in program
Storage Classes 
autokeyword 
Can used Explicitly 
auto double x; 
registerkeyword 
High speed register 
register double x = 4; 
statickeyword (Very important) 
function’s local variables 
Keeps values between functions call 
Known ONLY in ITS OWN function 
Static double x = 3; 
extern key word 
Global variables accessible to functions in Same file, Other files 
Must be declared in each file which has been used in. 
Used to access Global variable in another file
Storage Classes 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
auto register double x = 4; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
register auto double x = 4; 
} 
Compiler error, can’t use both at the same time 
Compiler error, can’t use both at the same time 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
auto double x = 4; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
register double x = 4; 
} 
Compile and run 
Compile and run
StaticStorage (Very important) 
#include <iostream> 
using namespace std; 
void StaticMyHeadache() 
{ 
static intx = 8; 
cout<< "In function, x = " << x << endl; 
x*=2; 
cout<< "In function, x = " << x << endl; 
} 
intmain() 
{ 
intx = 3; 
cout<< x << endl; 
StaticMyHeadache(); 
cout<< x << endl; 
StaticMyHeadache(); 
} 
Defined and initialized at the same time in the first time of calling the function then the compiler no longer parse this line of code 
3 
In function, x = 8 
In function, x = 16 
3 
In function, x = 16 
In function, x = 32 
Press any key to continue
extern Storage 
•1stfile 
•2ndfile 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intMyGlobalVar; 
} 
#include<iostream> 
usingnamespace::std; 
extern intMyGlobalVar;
extern Storage 
•1st file 
•2ndfile 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intMyGlobalVar; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
extern intMyGlobalVar; 
} 
Compiler error, why? 
2 “main”s in the same project! that’s wrong! Any project in the world will only have one main
inline
InlineFor small, common-used functionsNo function call, just copy the code into the programCompiler can ignore Inline
Inline functions 
#include<iostream> 
usingnamespace::std; 
intMultiplyByFive(int); 
voidmain( ) 
{ 
intx; 
cout << "Enter a number: "; 
cin>>x; 
cout<<"n The Output is: "<< MultiplyByFive(x) << endl; 
} 
inlineintMultiplyByFive (intx1) 
{ 
return5*x1; 
} 
Enter a number: 2 
The Output is: 10 
Press any key to continue
Blocks and Scopes
Blocks and Scopes 
•Scopes 
–Local Scopes 
–Global Scopes 
•The Golden Rule 
–Life time of block scope variables is lifetime of block 
–Variables are stored in a memory stack
Variables are stored in a stack
Variables are stored in a stack 
That means, first in Last OUT and VICE VERSA!
Blocks and Scopes 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
cout << "This is inner block "<< endl; 
} 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
cout << x << endl; 
} 
} 
This is inner block 
9
Blocks and Scopes 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
cout << x << endl; 
} 
cout << x << endl; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
intx = 4; 
cout << x << endl; 
} 
} 
9 
9 
4
Blocks and Scopes 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
intx; 
x = 4; 
cout << x << endl; 
} 
cout << x << endl; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
intx = 4; 
cout << x << endl; 
} 
cout << y << endl; 
} 
4 
9 
4 
3
Blocks and Scopes 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intx = 9; 
{ 
intx = 4, y = 3; 
cout << x << endl; 
} 
cout << y << endl; 
} 
Compiler error, y undeclared identifier
Blocks and Scopes 
#include<iostream> 
usingnamespace::std; 
intx = 35;// Global Variable 
voidbar() 
{ 
intx = 3;// local Variable 
} 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
intx = 6;// local Variable 
} 
cout << x << endl; 
} 
#include<iostream> 
usingnamespace::std; 
intx = 35;//Global Variable 
voidbar() 
{ 
intx = 3;// local Variable 
} 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
intx = 6;// local Variable 
} 
cout <<::x << endl; 
} 
9 
35
Blocks and Scopes 
#include<iostream> 
usingnamespace::std; 
intx = 34;// Global Variable 
voidbar() 
{ 
intx = 3;// local Variable 
cout <<::x << endl; 
} 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
intx = 6;// local Variable 
} 
} 
#include<iostream> 
usingnamespace::std; 
intx = 34;// Global Variable 
voidbar() 
{ 
intx = 3;// local Variable 
cout << x << endl; 
} 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
intx = 6;// local Variable 
} 
bar(); 
} 
Compile & Run 
3
Blocks and Scopes 
#include<iostream> 
usingnamespace::std; 
intx = 34;// Global Variable 
voidbar() 
{ 
intx = 3;// local Variable 
cout << ++::x << endl; 
} 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
intx = 6;// local Variable 
} 
bar(); 
} 
#include<iostream> 
usingnamespace::std; 
intx = 34;// Global Variable 
voidbar() 
{ 
intx = 3;// local Variable 
cout <<::x++ << endl; 
} 
voidmain(void) 
{ 
intx = 9, y = 3; 
{ 
intx = 6;// local Variable 
} 
bar(); 
cout <<::x << endl; 
} 
35 
34 
35
Math Library Functions
Quiz
Quiz -1 
#include<iostream> 
usingnamespace::std; 
intx = 34; 
voidfoo() 
{ 
staticintx = 3;// initialized only ONE time 
cout << "first x in foo = "<< x << endl; 
x++; 
cout << "second x in foo = "<< x << endl; 
} 
voidmain(void) 
{ 
intx = 9; 
cout << x << endl; 
{ 
intx = 5; 
cout << x << endl; 
} 
cout << x << endl; 
foo(); 
foo(); 
cout << x << endl; 
cout <<::x << endl; 
} 
9 
5 
9 
first x in foo = 3 
second x in foo = 4 
first x in foo = 4 
second x in foo = 5 
9 
34 
Press any key to continue
Quiz -2 
#include <iostream> 
using namespace::std; 
intx = 34; 
void foo() 
{ 
static intx = 3;// initialized only ONE time 
x = 5; 
cout<< "first x in foo = " << x << endl; 
x++; 
cout<< "second x in foo = " << x << endl; 
} 
voidmain(void) 
{ 
intx = 9; 
cout << x << endl; 
{ 
intx = 5; 
cout << x << endl; 
} 
cout << x << endl; 
foo(); 
foo(); 
cout << x << endl; 
cout <<::x << endl; 
} 
9 
5 
9 
first x in foo = 5 
second x in foo = 6 
first x in foo = 5 
second x in foo = 6 
9 
34 
Press any key to continue
Quiz –3, 4 
#include<iostream> 
usingnamespace::std; 
intx = 34; 
voidfoo() 
{ 
static intx = 3; 
intx = 3; 
cout << "first x in foo = "<< x << endl; 
x++; 
cout << "second x in foo = "<< x << endl; 
} 
Does this function runs? 
Compiler error, redefinition of x 
#include<iostream> 
usingnamespace::std; 
intx = 34; 
voidfoo() 
{ 
staticx = 3; 
x = 5; 
cout << "first x in foo = "<< x << endl; 
x++; 
cout << "second x in foo = "<< x << endl; 
} 
Does this function runs? 
Compiler error, missing type after static
Functions Default Parameters
Default Parameters 
#include<iostream> 
usingnamespace::std; 
voidprint(intx=0, intn=4 ) 
{ 
cout << x << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
2 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx=0, intn ) 
{ 
cout << x << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
Compiler error, missing default value for parameter 2 
When defaultinga parameter,all other parameters -if exist -on the right sideshould be defaulted too
Default Parameters 
#include<iostream> 
usingnamespace::std; 
voidprint(intx, intn=0 ) 
{ 
cout << x << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
2 
#include<iostream> 
usingnamespace::std; 
voidprint(intx, intn, inty ) 
{ 
cout << x << endl; 
} 
voidmain(void) 
{ 
print(2,3,4); 
} 
2
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn, inty = 0 ) 
{ 
cout << x << endl; 
} 
voidmain(void) 
{ 
print(2,3,4); 
} 
2 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn, inty = 0 ) 
{ 
cout << x << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
2
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint (intx, intn, inty=10 ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
2 
3 
10
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn=10, inty ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
Compiler error. y is not defaulted.
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn=10, inty ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
Compiler error. y is not defaulted.
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn=10, inty ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
Compiler error. y is not defaulted.
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn=10, inty ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
Compiler error. y is not defaulted.
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn=10, inty ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
Compiler error. y is not defaulted.
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn=10, inty ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
Compiler error. y is not defaulted.
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn=10, inty ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
Compiler error. y is not defaulted.
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn=10, inty ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
Compiler error. y is not defaulted. 
The compiler doesn’t know what to do? 
Should it replace the value (3) with the default value of the defaulted parameter (n) or go on to the next parameter (y) and pass it through
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn=10, inty ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
Compiler error. y is not defaulted. 
And nowyou can know whythe compiler can parse the function calling correctly when the function header have the defaulted parameter at the right most of the prototype and not in the middle 
coz as you saw, the compiler couldn’t determine where to pass the value! 
The compiler doesn’t know what to do? 
Should it replace the value (3) with the default value of the defaulted parameter (n) or go on to the next parameter (y) and pass it through
Default Parameters 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidprint(intx, intn=10, inty = 5 ) 
{ 
cout << x << endl; 
cout << n << endl; 
cout << y << endl; 
} 
voidmain(void) 
{ 
print(2,3); 
} 
2 
3 
5
Calling by valueVS calling by reference
Calling by value VS calling by reference 
•Calling by value 
–Copy of data 
–Changes to copy don’t affect original 
–Prevent an unwanted side effect 
•Calling by reference (&) 
–Function directly access data 
–Changes affect original (no copy exist here!) 
–Using & after data type 
•void foo(double & x)
Calling by value VS calling by reference 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidFuncByValue (intx ) 
{ 
cout << "In function before multiplication, x = "<< x << endl; 
x = x*4; 
cout << "In function after multiplication, x = "<< x << endl; 
} 
voidmain(void) 
{ 
intx = 2; 
cout << "In main, before calling the function "<< x << endl; 
FuncByValue(x); 
cout << "In main, after calling the function "<< x << endl; 
} 
In main, before calling the function 2 
In function before multiplication, x = 2 
In function after multiplication, x = 8 
In main, after calling the function 2 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidFuncByRef (int& x ) 
{ 
cout << "In function before multiplication, x = "<< x << endl; 
x = x*4; 
cout << "In function after multiplication, x = "<< x << endl; 
} 
voidmain(void) 
{ 
intx = 2; 
cout << "In main, before calling the function "<< x << endl; 
FuncByRef (x); 
cout << "In main, after calling the function "<< x << endl; 
} 
In main, before calling the function 2 
In function before multiplication, x = 2 
In function after multiplication, x = 8 
In main, after calling the function 8 
Press any key to continue
Calling by value VS calling by reference 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidFuncByRef (& intx ) 
{ 
cout << "In function before multiplication, x = "<< x << endl; 
x = x*4; 
cout << "In function after multiplication, x = "<< x << endl; 
} 
voidmain(void) 
{ 
intx = 2; 
cout << "In main, before calling the function "<< x << endl; 
FuncByRef (x); 
cout << "In main, after calling the function "<< x << endl; 
} 
Compiler error, & before type 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
voidFuncByRef ( intx & ) 
{ 
cout << "In function before multiplication, x = "<< x << endl; 
x = x*4; 
cout << "In function after multiplication, x = "<< x << endl; 
} 
voidmain(void) 
{ 
intx = 2; 
cout << "In main, before calling the function "<< x << endl; 
FuncByRef (x); 
cout << "In main, after calling the function "<< x << endl; 
} 
Compiler error, & after variable
Calling by value VS calling by reference 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
intFuncByValue ( intx ) 
{ 
cout << "In function before multiplication, x = "<< x << endl; 
x = x*4; 
cout << "In function after multiplication, x = "<< x << endl; 
returnx; 
} 
voidmain(void) 
{ 
intx = 2; 
cout << "In main, before calling the function, x = "<< x << endl; 
cout << FuncByValue(x) << endl; 
cout << "In main, after calling the function, x = "<< x << endl; 
} 
In main, before calling the function, x = 2 
In function before multiplication, x = 2 
In function after multiplication, x = 8 
8 
In main, after calling the function, x = 2 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
intFuncByRef ( int& x ) 
{ 
cout << "In function before multiplication, x = "<< x << endl; 
x = x*4; 
cout << "In function after multiplication, x = "<< x << endl; 
returnx; 
} 
voidmain(void) 
{ 
intx = 2; 
cout << "In main, before calling the function, x = "<< x << endl; 
cout << FuncByRef(x) << endl; 
cout << "In main, after calling the function, x = "<< x << endl; 
} 
In main, before calling the function, x = 2 
In function before multiplication, x = 2 
In function after multiplication, x = 8 
8 
In main, after calling the function, x = 8 
Press any key to continue
Function Overloading
Function Overloading 
•Is a function with 
–same name 
–different parameters 
–(Not the return type!) 
•That means that the overloaded functions are distinguished in Signature 
–(Not the return type!)
Function Overloading 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
intf ( int&x ) 
{ 
x = x * 2; 
returnx; 
} 
intf ( float&x ) 
{ 
x = x * 3; 
returnx; 
} 
voidmain(void) 
{ 
inty1 = 2; 
floaty2 = 2; 
cout << f(y1) << endl; 
cout << f(y2) << endl; 
} 
4 
6 
Oveloading but with warning (casting) 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
intf ( int&x ) 
{ 
x = x * 2; 
returnx; 
} 
float f ( float&x ) 
{ 
x = x * 3; 
returnx; 
} 
voidmain(void) 
{ 
inty1 = 2; 
floaty2 = 2; 
cout << f(y1) << endl; 
cout << f(y2) << endl; 
} 
4 
6 
No warnings
Function Overloading 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
intf ( intx ) 
{ 
x = x * 2; 
returnx; 
} 
float f ( floatx ) 
{ 
x = x * 3; 
returnx; 
} 
voidmain(void) 
{ 
inty1 = 2; 
floaty2 = 2; 
cout << f(y1) << endl; 
cout << f(y2) << endl; 
} 
4 
6 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
intf ( intx ) 
{ 
x = x * 2; 
returnx; 
} 
float f ( floatx ) 
{ 
x = x * 0; 
returnx; 
} 
voidmain(void) 
{ 
inty1 = 2; 
floaty2 = 2; 
cout << f(y1) << endl; 
cout << f(y2) << endl; 
} 
4 
0
Function Overloading 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
intf ( intx, intz) 
{ 
x = x * 2; 
cout << x << endl; 
cout << z << endl; 
returnx; 
} 
floatf (floatx, intz) 
{ 
x = x * 3; 
cout << x << endl; 
cout << z << endl; 
returnx; 
} 
voidmain(void) 
{ 
inty1 = 2; 
floaty2 = 2; 
intz1 = 3, z2 = 2; 
f(y1,z1); 
f(y2,z2); 
} 
4 
3 
6 
2 
No warnings 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
intf ( intx, intz) 
{ 
x = x * 2; 
cout << x << endl; 
cout << z << endl; 
returnx; 
} 
floatf (floatx, intz) 
{ 
x = x * 3; 
cout << x << endl; 
cout << z << endl; 
returnx; 
} 
voidmain(void) 
{ 
inty1 = 2; 
floaty2 = 2; 
float z1 = 3, z2 = 2; 
f(y1,z1); 
f(y2,z2); 
} 
4 
3 
6 
2 
Warnings
Function Overloading 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
intf ( intx, intz) 
{ 
x = x * 2; 
cout << x << endl; 
cout << z << endl; 
returnx; 
} 
floatf (intz, floatx) 
{ 
x = x * 3; 
cout << x << endl; 
cout << z << endl; 
returnx; 
} 
voidmain(void) 
{ 
inty1 = 2; 
floaty2 = 2; 
intz1 = 3, z2 = 2; 
f(y1,z1); 
f(y2,z2); 
} 
4 
3 
4 
2 
Warnings 
#include<iostream> 
usingnamespace::std; 
ints = 0; 
intf ( intx, intz) 
{ 
x = x * 2; 
cout << x << endl; 
cout << z << endl; 
returnx; 
} 
floatf (intz, floatx) 
{ 
x = x * 3; 
cout << x << endl; 
cout << z << endl; 
returnx; 
} 
voidmain(void) 
{ 
inty1 = 2; 
floaty2 = 2; 
float z1 = 3, z2 = 2; 
f(y1,z1); 
f(y2,z2); 
} 
9 
2 
6 
2 
Warnings
Function Overloading 
#include<iostream> 
usingnamespace::std; 
intx = 0; 
floatf2 (intz ) 
{ 
cout << z << endl; 
z*=2; 
returnz; 
} 
voidf1 ( int&x ) 
{ 
x+2; 
cout << x << endl; 
f2(x); 
cout << x << endl; 
} 
voidmain(void) 
{ 
f1(::x++); 
cout << ++::x << endl; 
} 
Compiler error Can’t convert from int to &int 
#include<iostream> 
usingnamespace::std; 
intx = 0; 
voidf1 ( int&x ) 
{ 
x+2; 
cout << x << endl; 
f2(x); 
cout << x << endl; 
} 
floatf2 (intz ) 
{ 
cout << z << endl; 
z*=2; 
returnz; 
} 
voidmain(void) 
{ 
f1(::x); 
cout <<::x << endl; 
} 
Compiler error. f1 can’t know what f2 is!
Quiz
Quiz 1 
voidmain(void) 
{ 
intx = 9; 
cout << x << endl; 
{ 
{ 
intx = 5; 
cout << x++ << endl; 
} 
cout << x << endl; 
::x = x+2; 
} 
cout << x << endl; 
{ 
foo(); 
foo();} 
cout << x << endl; 
cout <<::x << endl; 
} 
9 
5 
9 
9 
first x in foo = 5 
second x in foo = 6 
first x in foo = 5 
second x in foo = 6 
9 
11 
Press any key to continue 
#include <iostream> 
using namespace::std; 
intx = 34; 
void foo() 
{ 
static intx = 3;// initialized only ONE time 
x = 5; 
cout<< "first x in foo = " << x << endl; 
x++; 
cout<< "second x in foo = " << x << endl; 
}
Quiz 2 
#include<iostream> 
usingnamespace::std; 
voidTryMe(); 
voidmain() 
{ 
cout<<"In main"<<endl; 
TryMe(); 
} 
voidTryMe() 
{ 
cout<<"In function"<<endl; 
main(); 
} 
In main 
In function 
In main 
In function 
In main 
In function 
In main 
In function 
In main 
In function
Quiz 3 
#include<iostream> 
usingnamespace::std; 
voidTryMe() 
{ 
cout<<"In function"<<endl; 
main(); 
} 
voidmain() 
{ 
TryMe(); 
cout<<"In main"<<endl; 
} 
Compiler error! 
TryMe() can’t know what a main function is

More Related Content

PDF
C++ L03-Control Structure
PDF
C++ L07-Struct
PDF
C++ L01-Variables
PDF
C++ L04-Array+String
PDF
C++ L08-Classes Part1
PDF
C++ L02-Conversion+enum+Operators
PDF
C++ L06-Pointers
PDF
C++ L11-Polymorphism
C++ L03-Control Structure
C++ L07-Struct
C++ L01-Variables
C++ L04-Array+String
C++ L08-Classes Part1
C++ L02-Conversion+enum+Operators
C++ L06-Pointers
C++ L11-Polymorphism

What's hot (20)

PDF
C++ L09-Classes Part2
PDF
c programming
PPTX
Unit 3
PDF
c programming
PDF
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
PDF
Modern C++ Concurrency API
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
PDF
Recursion to iteration automation.
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
PDF
Imugi: Compiler made with Python
PPTX
C++ Lambda and concurrency
PPTX
C++ Pointers
PDF
C++ L10-Inheritance
PDF
C++ Programming - 11th Study
PDF
Антихрупкий TypeScript | Odessa Frontend Meetup #17
PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
PPT
Constructor,destructors cpp
DOC
C tech questions
PDF
Programming with GUTs
C++ L09-Classes Part2
c programming
Unit 3
c programming
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Modern C++ Concurrency API
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Recursion to iteration automation.
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Imugi: Compiler made with Python
C++ Lambda and concurrency
C++ Pointers
C++ L10-Inheritance
C++ Programming - 11th Study
Антихрупкий TypeScript | Odessa Frontend Meetup #17
Python Programming Essentials - M16 - Control Flow Statements and Loops
Constructor,destructors cpp
C tech questions
Programming with GUTs
Ad

Viewers also liked (20)

PDF
Netw450 advanced network security with lab entire class
PPTX
Functions c++ مشروع
PDF
Network-security muhibullah aman-first edition-in Persian
PPT
Functions in C++
PDF
Network Security in 2016
PPT
Functions in c++
PPTX
C++ lecture 03
DOCX
The Algebra of Functions
PPTX
Learning C++ - Functions in C++ 3
PDF
Basic openCV Functions Using CPP
PPTX
Functions in C++
PDF
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
PPT
Functions in c++
PPTX
C++ Programming Language Training in Ambala ! Batra Computer Centre
PPT
16717 functions in C++
 
PDF
Network Security Fundamentals
PPTX
C++ programming function
PPT
FUNCTIONS IN c++ PPT
PPT
Functions in C++
PPTX
Templates in C++
Netw450 advanced network security with lab entire class
Functions c++ مشروع
Network-security muhibullah aman-first edition-in Persian
Functions in C++
Network Security in 2016
Functions in c++
C++ lecture 03
The Algebra of Functions
Learning C++ - Functions in C++ 3
Basic openCV Functions Using CPP
Functions in C++
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Functions in c++
C++ Programming Language Training in Ambala ! Batra Computer Centre
16717 functions in C++
 
Network Security Fundamentals
C++ programming function
FUNCTIONS IN c++ PPT
Functions in C++
Templates in C++
Ad

Similar to C++ L05-Functions (20)

PDF
how to reuse code
PPT
Lecture05
PPT
Lecture#6 functions in c++
PPTX
Silde of the cse fundamentals a deep analysis
PPTX
functions of C++
PPTX
Cs1123 8 functions
PDF
PPT
PPTX
Chp7_C++_Functions_Part1_Built-in functions.pptx
PPTX
C++_Functions_Presentation of CS eve.pptx
PPT
PPT
Chapter 1.ppt
PPTX
Programming Fundamentals lecture-10.pptx
PPT
Fp201 unit5 1
PDF
Functions in c++
PPT
C++ Functions.ppt
PPT
power point presentation on object oriented programming functions concepts
PPTX
Functions in C++
PPTX
CPP Homework Help
PPTX
Chapter 4
how to reuse code
Lecture05
Lecture#6 functions in c++
Silde of the cse fundamentals a deep analysis
functions of C++
Cs1123 8 functions
Chp7_C++_Functions_Part1_Built-in functions.pptx
C++_Functions_Presentation of CS eve.pptx
Chapter 1.ppt
Programming Fundamentals lecture-10.pptx
Fp201 unit5 1
Functions in c++
C++ Functions.ppt
power point presentation on object oriented programming functions concepts
Functions in C++
CPP Homework Help
Chapter 4

More from Mohammad Shaker (20)

PDF
12 Rules You Should to Know as a Syrian Graduate
PDF
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
PDF
Interaction Design L06 - Tricks with Psychology
PDF
Short, Matters, Love - Passioneers Event 2015
PDF
Unity L01 - Game Development
PDF
Android L07 - Touch, Screen and Wearables
PDF
Interaction Design L03 - Color
PDF
Interaction Design L05 - Typography
PDF
Interaction Design L04 - Materialise and Coupling
PDF
Android L05 - Storage
PDF
Android L04 - Notifications and Threading
PDF
Android L09 - Windows Phone and iOS
PDF
Interaction Design L01 - Mobile Constraints
PDF
Interaction Design L02 - Pragnanz and Grids
PDF
Android L10 - Stores and Gaming
PDF
Android L06 - Cloud / Parse
PDF
Android L08 - Google Maps and Utilities
PDF
Android L03 - Styles and Themes
PDF
Android L02 - Activities and Adapters
PDF
Android L01 - Warm Up
12 Rules You Should to Know as a Syrian Graduate
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Interaction Design L06 - Tricks with Psychology
Short, Matters, Love - Passioneers Event 2015
Unity L01 - Game Development
Android L07 - Touch, Screen and Wearables
Interaction Design L03 - Color
Interaction Design L05 - Typography
Interaction Design L04 - Materialise and Coupling
Android L05 - Storage
Android L04 - Notifications and Threading
Android L09 - Windows Phone and iOS
Interaction Design L01 - Mobile Constraints
Interaction Design L02 - Pragnanz and Grids
Android L10 - Stores and Gaming
Android L06 - Cloud / Parse
Android L08 - Google Maps and Utilities
Android L03 - Styles and Themes
Android L02 - Activities and Adapters
Android L01 - Warm Up

Recently uploaded (20)

PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PDF
composite construction of structures.pdf
PDF
ETO & MEO Certificate of Competency Questions and Answers
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Road Safety tips for School Kids by a k maurya.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPT
Drone Technology Electronics components_1
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
PPT
Chapter 6 Design in software Engineeing.ppt
PPTX
Simulation of electric circuit laws using tinkercad.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
Lesson 3_Tessellation.pptx finite Mathematics
composite construction of structures.pdf
ETO & MEO Certificate of Competency Questions and Answers
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CH1 Production IntroductoryConcepts.pptx
Road Safety tips for School Kids by a k maurya.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Drone Technology Electronics components_1
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Structs to JSON How Go Powers REST APIs.pdf
Geotechnical Engineering, Soil mechanics- Soil Testing.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
오픈소스 LLM, vLLM으로 Production까지 (Instruct.KR Summer Meetup, 2025)
Chapter 6 Design in software Engineeing.ppt
Simulation of electric circuit laws using tinkercad.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Model Code of Practice - Construction Work - 21102022 .pdf

C++ L05-Functions

  • 1. Mohammad Shaker mohammadshaker.com @ZGTRShaker 2010, 11, 12, 13, 14 C++ Programming Language L04-FUNCTIONS
  • 2. Functions •Why function? •functions procedures in c++ •What’s the function prototype (declaration) definition? •Function signature (2010 exam Question) –What’s it? –It’s just the name of the function with its parameters •No return type!!!! (Return type is not considered as part of the function’s signature!, guess why?) intfoo(int);// function prototype foo(int) // function signature
  • 3. Functions #include<iostream> usingnamespace::std; intfoo(int);// function prototype intmain() { return0; } #include<iostream> usingnamespace::std; foo(int); // function prototype intmain() { return0; } Compiler error, missing return type
  • 4. Functions •The following are the same #include<iostream> usingnamespace::std; intMult( intx ) // note there's no; when we // write the definition here { x = x * 2; returnx; } void main() { } #include<iostream> usingnamespace::std; intMult( intx ); void main() {} intMult( intx ) { x = x * 2; returnx; }
  • 5. Functions •The following are the same #include<iostream> usingnamespace::std; intMult( int x ) { x = x * 2; returnx; } void main() { } #include<iostream> usingnamespace::std; intMult(int); // we didn’t write the x // pirmeted when prototype only void main() {} intMult( intx ) { x = x * 2; returnx; }
  • 6. Functions #include<iostream> usingnamespace::std; intMult(int); void main() { Mult (3); } intMult( intx ) { x = x * 2; returnx; } #include<iostream> usingnamespace::std; intMult(int); void main() { cout << Mult(3); } intMult( intx ) { x = x * 2; returnx; } 6
  • 7. Functions #include<iostream> usingnamespace::std; intMult(int); void main() { Mult(3); } intMult( intx ) { x = x * 2; cout << x; return0; } #include<iostream> usingnamespace::std; intMult(int); void main() { int c = 3; Mult(c); } intMult( intx ) { x = x * 2; cout << x; return0; } 6 6
  • 8. Functions #include<iostream> usingnamespace::std; intMult(int); void main() { int x = 3; Mult(x); } intMult( intx ) { x = x * 2; cout << x; return0; } #include<iostream> usingnamespace::std; intMult(int); void main() { int x = 3; Mult(x); cout << x; } intMult( intx ) { x = x * 2; cout << x; return0; } 6 6
  • 9. Functions #include<iostream> usingnamespace::std; intMult(int); void main() { int x = 3; Mult(x); cout << endl; cout << x; } intMult( intx ) { x = x * 2; cout << x; return0; } #include<iostream> usingnamespace::std; intMult(int); void main() { int x = 3; Mult(x); cout << x; } intMult( intx ) { x = x * 2; cout << x << endl; return0; } 6 3 6 3
  • 10. Functions #include<iostream> usingnamespace::std; intMult(int); void main() { int x = 3; cout << Mult(x) << endl; cout << x; } intMult( intx ) { x = x * 2; returnx; } #include<iostream> usingnamespace::std; void Mult(void); void main() { int x = 3; Mult() << endl; cout << x; } voidMult( void ) { int x = 3; cout << x * 2 << endl; } 6 3 Compiler error
  • 11. Functions #include<iostream> usingnamespace::std; void Mult(void); void main() { int c = 3; Mult() << endl; cout << c; } voidMult( void ) { int x = 3; cout << x * 2 << endl; } #include<iostream> usingnamespace::std; void Mult(void); void main() { Mult() << endl; } voidMult( void ) { int x = 3; x = x * 2; return0; } Compiler error Compiler error, return 0; with void function
  • 12. Functions #include<iostream> usingnamespace::std; void Mult(void); void main() { cout << Mult() << endl; } voidMult() { int x = 3; x = x * 2; } #include<iostream> usingnamespace::std; void Mult(void); void main(void) { Mult(); } voidMult(void) { int x = 3; x = x * 2; } Compiler error, cout with void return funtion At last everything’s working
  • 13. Functions #include<iostream> usingnamespace::std; void Mult(void); void main(void) { Mult(); } voidMult(void) { int x = 3; x = x * 2; } #include<iostream> usingnamespace::std; void Mult(); void main() { Mult(); } voidMult() { int x = 3; x = x * 2; } Compile and run Compile and run. In the parameter list, voidor empty is the same
  • 14. Functions #include<iostream> usingnamespace::std; voidMult(void); voidmain() { Mult(); } Mult() { int x = 3; x = x * 2; } #include<iostream> usingnamespace::std; Mult(void); voidmain() { Mult(); } void Mult() { int x = 3; x = x * 2; } Compiler error, missing return type Compiler error, missing return type
  • 15. Functions #include<iostream> usingnamespace::std; void Mult(void); void main(void) { Mult(); } int Mult(void) { int x = 3; x = x * 2; returnx; } #include<iostream> usingnamespace::std; int Mult(void); void main(void) { Mult(); } void Mult(void) { int x = 3; x = x * 2; } Compiler error, return type differs Compiler error, return type differs
  • 16. Functions #include<iostream> usingnamespace::std; int Mult(void); void main(void) { cout << Mult() << endl; } int Mult(void) { int x = 3; x = x * 2; returnx; } #include<iostream> usingnamespace::std; int Mult(void); void main(void) { cout << Mult() << endl; } int Mult(void) { int x = 3; returnx * 2; } 6 6
  • 17. Functions #include<iostream> usingnamespace::std; int Mult(void); void main(void) { cout << Mult() << endl; } int Mult(void) { int x = 3; returnx * 2; cout << x << end; } #include<iostream> usingnamespace::std; int Mult(int x); void main(void) { cout << Mult() << endl; } int Mult(int x) { returnx * 2; } 6 Compiler error, no variable passed to function Nothing got excuted after the return statement
  • 18. Functions #include<iostream> usingnamespace::std; int Mult(int x); void main(void) { cout << Mult(3) << endl; } int Mult(int x) { returnx * 2; } #include<iostream> usingnamespace::std; int Mult(int x); void main(void) { cout << Mult(3) << endl; } int Mult(int x) { intx = 2; returnx * 2; } 0 Compiler error, redefinition of variable x
  • 19. Functions #include<iostream> usingnamespace::std; intMult(int, int); voidmain(void) { inti1, i2; cout << Mult(i1,i2) << endl; } intMult(intx, inty) { returnx * 2; } #include<iostream> usingnamespace::std; intMult(int, int); voidmain(void) { inti1, i2; cout << Mult(i1,i2) << endl; } intMult(intx, y) { returnx * 2; } 6 Compiler error, missing type for y
  • 20. Functions #include<iostream> usingnamespace::std; intMult(int, int); voidmain(void) { inti1, i2; cout << Mult(i1,i2) << endl; } intMult(intx, inty) { if(x = 2 ) { return3; } else { returnx * 2; } } #include<iostream> usingnamespace::std; intMult(int, int); voidmain(void) { inti1, i2; cout << Mult(i1,i2) << endl; } intMult(intx, inty) { int MeMe (int z) { return 0; } } 3 Compiler error, can’t define a function inside another one
  • 21. Functions #include<iostream> usingnamespace::std; intMult(int, int); voidmain(void) { floati1= 3.4, i2; cout << Mult(i1,i2) << endl; } intMult(intx, inty) { return2*x; } #include<iostream> usingnamespace::std; intMult(int); voidmain(void) { for(inti = 0; i<=10; i++) { cout << Mult(i) << "-"; } cout << “hehe” << endl; cout<< endl; } intMult(intx ) { return2*x; } 6 0-2-4-6-8-10-12-14-16-18-20-hehe Press any key to continue
  • 22. Functions #include<iostream> usingnamespace::std; voidCrazy1(); voidCrazy2(int); voidmain(void) {Crazy1(); } voidCrazy1() { intx = 3; Crazy2(x); cout << x; } voidCrazy2(intx ) { x+=6; cout << x << endl; } 9 3
  • 25. Headers File •What’s a header? –Moving functions out side the main program •Logical groups –Stored in their own files •How to do it? –By Moving •function declarations –Into headers files »(.h) files // header files •function definitions –Into source files »(.cpp) files // source files –Note: function definitions can’t be split up into multiple files!
  • 27. cmathand sqrt #include<iostream> #include<cmath> usingnamespace::std; voidmain(void) { cout << sqrt(100.0) << endl; } #include<iostream> usingnamespace::std; voidmain(void) { cout << sqrt(100.0) << endl; } 10 Compiler error identifier not found •To use it –Include <cmath>
  • 28. cmathand sqrt #include<iostream> #include<cmath> usingnamespace::std; voidmain(void) { cout << sqrt(100) << endl; } #include<iostream> #include<cmath> usingnamespace::std; voidmain(void) { cout << sqrt(sqrt(100.0)) << endl; } Compiler error, can’t use integer “no overloaded function” 3.16228
  • 29. cmathand sqrt #include<iostream> #include<cmath> usingnamespace::std; voidmain(void) { intx; cout << sqrt(6*x) << endl; } #include<iostream> #include<cmath> usingnamespace::std; voidmain(void) { doublex = 3; cout << sqrt(3*x) << endl; } Compiler error, can’t use integer “no overloaded function” 3
  • 30. cmathand sqrt #include<iostream> #include<cmath> usingnamespace::std; voidmain(void) { float x = 3; cout << sqrt(3*x) << endl; } 3
  • 32. rand() •Random number –rand() –Needs to include <cstdlib> •Generates unsigned integers –Between »0 (Zero :D) »Rand_Maxnumber (usually 32767)
  • 33. rand() #include<iostream> #include <cstdlib> usingnamespace::std; voidmain(void) { intx = 3; x = rand(); cout << x << endl; } 41 Now, when compiling many times Every time we have the same random value! 41 41 41
  • 34. rand() #include<iostream> usingnamespace::std; voidmain(void) { intx = 3; x = rand(); cout << x << endl; } #include<iostream> #include <cstdlib> usingnamespace::std; voidmain(void) { intx = 3; x = rand(); cout << x << endl; cout << x << endl; } 54 156 156 Works without <cstlib>
  • 35. rand() #include<iostream> #include <cstdlib> usingnamespace::std; voidmain(void) { intx = 3; x = rand(); cout << x << endl; x = rand(); cout << x << endl; } 156 12356
  • 37. srand() •srand() –#include <cstdlib> –Give a starting value for the rand()function –Usually used once in program
  • 38. srand() #include<iostream> #include<ctime> usingnamespace::std; voidmain(void) { intx = 3; srand (time(0)); cout << x << endl; } #include<iostream> #include<ctime> usingnamespace::std; voidmain(void) { intx = 3; srand (time()); cout << x << endl; } 3 Compiler error, need value for time()
  • 39. What happened when compiling many times? srand() #include<iostream> #include<ctime> usingnamespace::std; voidmain(void) { intx = 3; srand (time(0)); x = rand (); cout << x << endl; } 456 Now, when compiling many times. Every time we have a new differentvalue 12345 189 What happened when compiling many times? #include<iostream> #include<ctime> usingnamespace::std; voidmain(void) { intx = 3; srand (5); x = rand (); cout << x << endl; } 48 Not an appropriate srand() paramater leads to insufficient using of srand()!!! 48 48
  • 40. srand() with clock() #include<iostream> #include<ctime> usingnamespace::std; voidmain(void) { srand(clock()); // clock is from <ctime> // no needs to value for clock () for(inti = 0; i < 10; i++) cout << rand() << endl; } 714 4415 16337 2807 14052 5430 30050 16163 20477 3146 Press any key to continue
  • 41. rand() •Scaling & shifting –% •x % y // returns a value between 0 to y-1 •let’s see an example #include<iostream> #include<cstdlib> usingnamespace::std; voidmain(void) { inti; i = rand() % 6 + 1; // rand() % 6: number between 0 to 5 // +1: makes a shift of the range we have // 0 to 5 becomes 1 to 6 cout << i << endl; // here will print a number absolutely between 1 to 6 } 6 Or any other number between 1 and 6
  • 43. Variables and Storage •Name, type, size, values •Storage class –Period variable living in memory •Linkage –Multiple-file processing, which files can use it. •Scope –Variable can be referenced in program
  • 44. Storage Classes autokeyword Can used Explicitly auto double x; registerkeyword High speed register register double x = 4; statickeyword (Very important) function’s local variables Keeps values between functions call Known ONLY in ITS OWN function Static double x = 3; extern key word Global variables accessible to functions in Same file, Other files Must be declared in each file which has been used in. Used to access Global variable in another file
  • 45. Storage Classes #include<iostream> usingnamespace::std; voidmain(void) { auto register double x = 4; } #include<iostream> usingnamespace::std; voidmain(void) { register auto double x = 4; } Compiler error, can’t use both at the same time Compiler error, can’t use both at the same time #include<iostream> usingnamespace::std; voidmain(void) { auto double x = 4; } #include<iostream> usingnamespace::std; voidmain(void) { register double x = 4; } Compile and run Compile and run
  • 46. StaticStorage (Very important) #include <iostream> using namespace std; void StaticMyHeadache() { static intx = 8; cout<< "In function, x = " << x << endl; x*=2; cout<< "In function, x = " << x << endl; } intmain() { intx = 3; cout<< x << endl; StaticMyHeadache(); cout<< x << endl; StaticMyHeadache(); } Defined and initialized at the same time in the first time of calling the function then the compiler no longer parse this line of code 3 In function, x = 8 In function, x = 16 3 In function, x = 16 In function, x = 32 Press any key to continue
  • 47. extern Storage •1stfile •2ndfile #include<iostream> usingnamespace::std; voidmain(void) { intMyGlobalVar; } #include<iostream> usingnamespace::std; extern intMyGlobalVar;
  • 48. extern Storage •1st file •2ndfile #include<iostream> usingnamespace::std; voidmain(void) { intMyGlobalVar; } #include<iostream> usingnamespace::std; voidmain(void) { extern intMyGlobalVar; } Compiler error, why? 2 “main”s in the same project! that’s wrong! Any project in the world will only have one main
  • 50. InlineFor small, common-used functionsNo function call, just copy the code into the programCompiler can ignore Inline
  • 51. Inline functions #include<iostream> usingnamespace::std; intMultiplyByFive(int); voidmain( ) { intx; cout << "Enter a number: "; cin>>x; cout<<"n The Output is: "<< MultiplyByFive(x) << endl; } inlineintMultiplyByFive (intx1) { return5*x1; } Enter a number: 2 The Output is: 10 Press any key to continue
  • 53. Blocks and Scopes •Scopes –Local Scopes –Global Scopes •The Golden Rule –Life time of block scope variables is lifetime of block –Variables are stored in a memory stack
  • 54. Variables are stored in a stack
  • 55. Variables are stored in a stack That means, first in Last OUT and VICE VERSA!
  • 56. Blocks and Scopes #include<iostream> usingnamespace::std; voidmain(void) { intx = 9, y = 3; { cout << "This is inner block "<< endl; } } #include<iostream> usingnamespace::std; voidmain(void) { intx = 9, y = 3; { cout << x << endl; } } This is inner block 9
  • 57. Blocks and Scopes #include<iostream> usingnamespace::std; voidmain(void) { intx = 9, y = 3; { cout << x << endl; } cout << x << endl; } #include<iostream> usingnamespace::std; voidmain(void) { intx = 9, y = 3; { intx = 4; cout << x << endl; } } 9 9 4
  • 58. Blocks and Scopes #include<iostream> usingnamespace::std; voidmain(void) { intx = 9, y = 3; { intx; x = 4; cout << x << endl; } cout << x << endl; } #include<iostream> usingnamespace::std; voidmain(void) { intx = 9, y = 3; { intx = 4; cout << x << endl; } cout << y << endl; } 4 9 4 3
  • 59. Blocks and Scopes #include<iostream> usingnamespace::std; voidmain(void) { intx = 9; { intx = 4, y = 3; cout << x << endl; } cout << y << endl; } Compiler error, y undeclared identifier
  • 60. Blocks and Scopes #include<iostream> usingnamespace::std; intx = 35;// Global Variable voidbar() { intx = 3;// local Variable } voidmain(void) { intx = 9, y = 3; { intx = 6;// local Variable } cout << x << endl; } #include<iostream> usingnamespace::std; intx = 35;//Global Variable voidbar() { intx = 3;// local Variable } voidmain(void) { intx = 9, y = 3; { intx = 6;// local Variable } cout <<::x << endl; } 9 35
  • 61. Blocks and Scopes #include<iostream> usingnamespace::std; intx = 34;// Global Variable voidbar() { intx = 3;// local Variable cout <<::x << endl; } voidmain(void) { intx = 9, y = 3; { intx = 6;// local Variable } } #include<iostream> usingnamespace::std; intx = 34;// Global Variable voidbar() { intx = 3;// local Variable cout << x << endl; } voidmain(void) { intx = 9, y = 3; { intx = 6;// local Variable } bar(); } Compile & Run 3
  • 62. Blocks and Scopes #include<iostream> usingnamespace::std; intx = 34;// Global Variable voidbar() { intx = 3;// local Variable cout << ++::x << endl; } voidmain(void) { intx = 9, y = 3; { intx = 6;// local Variable } bar(); } #include<iostream> usingnamespace::std; intx = 34;// Global Variable voidbar() { intx = 3;// local Variable cout <<::x++ << endl; } voidmain(void) { intx = 9, y = 3; { intx = 6;// local Variable } bar(); cout <<::x << endl; } 35 34 35
  • 64. Quiz
  • 65. Quiz -1 #include<iostream> usingnamespace::std; intx = 34; voidfoo() { staticintx = 3;// initialized only ONE time cout << "first x in foo = "<< x << endl; x++; cout << "second x in foo = "<< x << endl; } voidmain(void) { intx = 9; cout << x << endl; { intx = 5; cout << x << endl; } cout << x << endl; foo(); foo(); cout << x << endl; cout <<::x << endl; } 9 5 9 first x in foo = 3 second x in foo = 4 first x in foo = 4 second x in foo = 5 9 34 Press any key to continue
  • 66. Quiz -2 #include <iostream> using namespace::std; intx = 34; void foo() { static intx = 3;// initialized only ONE time x = 5; cout<< "first x in foo = " << x << endl; x++; cout<< "second x in foo = " << x << endl; } voidmain(void) { intx = 9; cout << x << endl; { intx = 5; cout << x << endl; } cout << x << endl; foo(); foo(); cout << x << endl; cout <<::x << endl; } 9 5 9 first x in foo = 5 second x in foo = 6 first x in foo = 5 second x in foo = 6 9 34 Press any key to continue
  • 67. Quiz –3, 4 #include<iostream> usingnamespace::std; intx = 34; voidfoo() { static intx = 3; intx = 3; cout << "first x in foo = "<< x << endl; x++; cout << "second x in foo = "<< x << endl; } Does this function runs? Compiler error, redefinition of x #include<iostream> usingnamespace::std; intx = 34; voidfoo() { staticx = 3; x = 5; cout << "first x in foo = "<< x << endl; x++; cout << "second x in foo = "<< x << endl; } Does this function runs? Compiler error, missing type after static
  • 69. Default Parameters #include<iostream> usingnamespace::std; voidprint(intx=0, intn=4 ) { cout << x << endl; } voidmain(void) { print(2,3); } 2 #include<iostream> usingnamespace::std; ints = 0; voidprint(intx=0, intn ) { cout << x << endl; } voidmain(void) { print(2,3); } Compiler error, missing default value for parameter 2 When defaultinga parameter,all other parameters -if exist -on the right sideshould be defaulted too
  • 70. Default Parameters #include<iostream> usingnamespace::std; voidprint(intx, intn=0 ) { cout << x << endl; } voidmain(void) { print(2,3); } 2 #include<iostream> usingnamespace::std; voidprint(intx, intn, inty ) { cout << x << endl; } voidmain(void) { print(2,3,4); } 2
  • 71. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn, inty = 0 ) { cout << x << endl; } voidmain(void) { print(2,3,4); } 2 #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn, inty = 0 ) { cout << x << endl; } voidmain(void) { print(2,3); } 2
  • 72. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint (intx, intn, inty=10 ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } 2 3 10
  • 73. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn=10, inty ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } Compiler error. y is not defaulted.
  • 74. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn=10, inty ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } Compiler error. y is not defaulted.
  • 75. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn=10, inty ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } Compiler error. y is not defaulted.
  • 76. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn=10, inty ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } Compiler error. y is not defaulted.
  • 77. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn=10, inty ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } Compiler error. y is not defaulted.
  • 78. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn=10, inty ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } Compiler error. y is not defaulted.
  • 79. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn=10, inty ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } Compiler error. y is not defaulted.
  • 80. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn=10, inty ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } Compiler error. y is not defaulted. The compiler doesn’t know what to do? Should it replace the value (3) with the default value of the defaulted parameter (n) or go on to the next parameter (y) and pass it through
  • 81. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn=10, inty ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } Compiler error. y is not defaulted. And nowyou can know whythe compiler can parse the function calling correctly when the function header have the defaulted parameter at the right most of the prototype and not in the middle coz as you saw, the compiler couldn’t determine where to pass the value! The compiler doesn’t know what to do? Should it replace the value (3) with the default value of the defaulted parameter (n) or go on to the next parameter (y) and pass it through
  • 82. Default Parameters #include<iostream> usingnamespace::std; ints = 0; voidprint(intx, intn=10, inty = 5 ) { cout << x << endl; cout << n << endl; cout << y << endl; } voidmain(void) { print(2,3); } 2 3 5
  • 83. Calling by valueVS calling by reference
  • 84. Calling by value VS calling by reference •Calling by value –Copy of data –Changes to copy don’t affect original –Prevent an unwanted side effect •Calling by reference (&) –Function directly access data –Changes affect original (no copy exist here!) –Using & after data type •void foo(double & x)
  • 85. Calling by value VS calling by reference #include<iostream> usingnamespace::std; ints = 0; voidFuncByValue (intx ) { cout << "In function before multiplication, x = "<< x << endl; x = x*4; cout << "In function after multiplication, x = "<< x << endl; } voidmain(void) { intx = 2; cout << "In main, before calling the function "<< x << endl; FuncByValue(x); cout << "In main, after calling the function "<< x << endl; } In main, before calling the function 2 In function before multiplication, x = 2 In function after multiplication, x = 8 In main, after calling the function 2 Press any key to continue #include<iostream> usingnamespace::std; ints = 0; voidFuncByRef (int& x ) { cout << "In function before multiplication, x = "<< x << endl; x = x*4; cout << "In function after multiplication, x = "<< x << endl; } voidmain(void) { intx = 2; cout << "In main, before calling the function "<< x << endl; FuncByRef (x); cout << "In main, after calling the function "<< x << endl; } In main, before calling the function 2 In function before multiplication, x = 2 In function after multiplication, x = 8 In main, after calling the function 8 Press any key to continue
  • 86. Calling by value VS calling by reference #include<iostream> usingnamespace::std; ints = 0; voidFuncByRef (& intx ) { cout << "In function before multiplication, x = "<< x << endl; x = x*4; cout << "In function after multiplication, x = "<< x << endl; } voidmain(void) { intx = 2; cout << "In main, before calling the function "<< x << endl; FuncByRef (x); cout << "In main, after calling the function "<< x << endl; } Compiler error, & before type #include<iostream> usingnamespace::std; ints = 0; voidFuncByRef ( intx & ) { cout << "In function before multiplication, x = "<< x << endl; x = x*4; cout << "In function after multiplication, x = "<< x << endl; } voidmain(void) { intx = 2; cout << "In main, before calling the function "<< x << endl; FuncByRef (x); cout << "In main, after calling the function "<< x << endl; } Compiler error, & after variable
  • 87. Calling by value VS calling by reference #include<iostream> usingnamespace::std; ints = 0; intFuncByValue ( intx ) { cout << "In function before multiplication, x = "<< x << endl; x = x*4; cout << "In function after multiplication, x = "<< x << endl; returnx; } voidmain(void) { intx = 2; cout << "In main, before calling the function, x = "<< x << endl; cout << FuncByValue(x) << endl; cout << "In main, after calling the function, x = "<< x << endl; } In main, before calling the function, x = 2 In function before multiplication, x = 2 In function after multiplication, x = 8 8 In main, after calling the function, x = 2 Press any key to continue #include<iostream> usingnamespace::std; ints = 0; intFuncByRef ( int& x ) { cout << "In function before multiplication, x = "<< x << endl; x = x*4; cout << "In function after multiplication, x = "<< x << endl; returnx; } voidmain(void) { intx = 2; cout << "In main, before calling the function, x = "<< x << endl; cout << FuncByRef(x) << endl; cout << "In main, after calling the function, x = "<< x << endl; } In main, before calling the function, x = 2 In function before multiplication, x = 2 In function after multiplication, x = 8 8 In main, after calling the function, x = 8 Press any key to continue
  • 89. Function Overloading •Is a function with –same name –different parameters –(Not the return type!) •That means that the overloaded functions are distinguished in Signature –(Not the return type!)
  • 90. Function Overloading #include<iostream> usingnamespace::std; ints = 0; intf ( int&x ) { x = x * 2; returnx; } intf ( float&x ) { x = x * 3; returnx; } voidmain(void) { inty1 = 2; floaty2 = 2; cout << f(y1) << endl; cout << f(y2) << endl; } 4 6 Oveloading but with warning (casting) #include<iostream> usingnamespace::std; ints = 0; intf ( int&x ) { x = x * 2; returnx; } float f ( float&x ) { x = x * 3; returnx; } voidmain(void) { inty1 = 2; floaty2 = 2; cout << f(y1) << endl; cout << f(y2) << endl; } 4 6 No warnings
  • 91. Function Overloading #include<iostream> usingnamespace::std; ints = 0; intf ( intx ) { x = x * 2; returnx; } float f ( floatx ) { x = x * 3; returnx; } voidmain(void) { inty1 = 2; floaty2 = 2; cout << f(y1) << endl; cout << f(y2) << endl; } 4 6 #include<iostream> usingnamespace::std; ints = 0; intf ( intx ) { x = x * 2; returnx; } float f ( floatx ) { x = x * 0; returnx; } voidmain(void) { inty1 = 2; floaty2 = 2; cout << f(y1) << endl; cout << f(y2) << endl; } 4 0
  • 92. Function Overloading #include<iostream> usingnamespace::std; ints = 0; intf ( intx, intz) { x = x * 2; cout << x << endl; cout << z << endl; returnx; } floatf (floatx, intz) { x = x * 3; cout << x << endl; cout << z << endl; returnx; } voidmain(void) { inty1 = 2; floaty2 = 2; intz1 = 3, z2 = 2; f(y1,z1); f(y2,z2); } 4 3 6 2 No warnings #include<iostream> usingnamespace::std; ints = 0; intf ( intx, intz) { x = x * 2; cout << x << endl; cout << z << endl; returnx; } floatf (floatx, intz) { x = x * 3; cout << x << endl; cout << z << endl; returnx; } voidmain(void) { inty1 = 2; floaty2 = 2; float z1 = 3, z2 = 2; f(y1,z1); f(y2,z2); } 4 3 6 2 Warnings
  • 93. Function Overloading #include<iostream> usingnamespace::std; ints = 0; intf ( intx, intz) { x = x * 2; cout << x << endl; cout << z << endl; returnx; } floatf (intz, floatx) { x = x * 3; cout << x << endl; cout << z << endl; returnx; } voidmain(void) { inty1 = 2; floaty2 = 2; intz1 = 3, z2 = 2; f(y1,z1); f(y2,z2); } 4 3 4 2 Warnings #include<iostream> usingnamespace::std; ints = 0; intf ( intx, intz) { x = x * 2; cout << x << endl; cout << z << endl; returnx; } floatf (intz, floatx) { x = x * 3; cout << x << endl; cout << z << endl; returnx; } voidmain(void) { inty1 = 2; floaty2 = 2; float z1 = 3, z2 = 2; f(y1,z1); f(y2,z2); } 9 2 6 2 Warnings
  • 94. Function Overloading #include<iostream> usingnamespace::std; intx = 0; floatf2 (intz ) { cout << z << endl; z*=2; returnz; } voidf1 ( int&x ) { x+2; cout << x << endl; f2(x); cout << x << endl; } voidmain(void) { f1(::x++); cout << ++::x << endl; } Compiler error Can’t convert from int to &int #include<iostream> usingnamespace::std; intx = 0; voidf1 ( int&x ) { x+2; cout << x << endl; f2(x); cout << x << endl; } floatf2 (intz ) { cout << z << endl; z*=2; returnz; } voidmain(void) { f1(::x); cout <<::x << endl; } Compiler error. f1 can’t know what f2 is!
  • 95. Quiz
  • 96. Quiz 1 voidmain(void) { intx = 9; cout << x << endl; { { intx = 5; cout << x++ << endl; } cout << x << endl; ::x = x+2; } cout << x << endl; { foo(); foo();} cout << x << endl; cout <<::x << endl; } 9 5 9 9 first x in foo = 5 second x in foo = 6 first x in foo = 5 second x in foo = 6 9 11 Press any key to continue #include <iostream> using namespace::std; intx = 34; void foo() { static intx = 3;// initialized only ONE time x = 5; cout<< "first x in foo = " << x << endl; x++; cout<< "second x in foo = " << x << endl; }
  • 97. Quiz 2 #include<iostream> usingnamespace::std; voidTryMe(); voidmain() { cout<<"In main"<<endl; TryMe(); } voidTryMe() { cout<<"In function"<<endl; main(); } In main In function In main In function In main In function In main In function In main In function
  • 98. Quiz 3 #include<iostream> usingnamespace::std; voidTryMe() { cout<<"In function"<<endl; main(); } voidmain() { TryMe(); cout<<"In main"<<endl; } Compiler error! TryMe() can’t know what a main function is