KEMBAR78
C++ L05-Functions | PDF
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

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 followingare 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 followingare 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
  • 23.
  • 24.
  • 25.
    Headers File •What’sa 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!
  • 26.
  • 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
  • 31.
  • 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
  • 36.
  • 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 whencompiling 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
  • 42.
  • 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 •1stfile •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
  • 49.
  • 50.
    InlineFor small, common-usedfunctionsNo 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
  • 52.
  • 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.
  • 55.
    Variables are storedin 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
  • 63.
  • 64.
  • 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
  • 68.
  • 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 valueVScalling by reference
  • 84.
    Calling by valueVS 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 valueVS 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 valueVS 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 valueVS 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
  • 88.
  • 89.
    Function Overloading •Isa 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.
  • 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