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
Ad

More Related Content

What's hot (20)

C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
Mohammad Shaker
 
c programming
c programmingc programming
c programming
Arun Umrao
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
c programming
c programmingc programming
c programming
Arun Umrao
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency API
Seok-joon Yun
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
ssuserd6b1fd
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
Russell Childs
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
ssuserd6b1fd
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
Han Lee
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
명신 김
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
Chaand Sheikh
 
C++ L10-Inheritance
C++ L10-InheritanceC++ L10-Inheritance
C++ L10-Inheritance
Mohammad Shaker
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
Chris Ohk
 
Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17
OdessaFrontend
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
P3 InfoTech Solutions Pvt. Ltd.
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
रमन सनौरिया
 
C tech questions
C tech questionsC tech questions
C tech questions
vijay00791
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
Kevlin Henney
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
ssuserd6b1fd
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
ssuserd6b1fd
 
Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency API
Seok-joon Yun
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
ssuserd6b1fd
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
Russell Childs
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
ssuserd6b1fd
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
Han Lee
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
명신 김
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
Chris Ohk
 
Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17
OdessaFrontend
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
P3 InfoTech Solutions Pvt. Ltd.
 
C tech questions
C tech questionsC tech questions
C tech questions
vijay00791
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
Kevlin Henney
 

Viewers also liked (20)

Netw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classNetw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire class
EugenioBrown1
 
Functions c++ مشروع
Functions c++ مشروعFunctions c++ مشروع
Functions c++ مشروع
ziadalmulla
 
Network-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianNetwork-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in Persian
Muhibullah Aman
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Mohammed Sikander
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016
Qrator Labs
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Maaz Hasan
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
HNDE Labuduwa Galle
 
The Algebra of Functions
The Algebra of FunctionsThe Algebra of Functions
The Algebra of Functions
Christopher Gratton
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
Ali Aminian
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPP
Wei-Wen Hsu
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You ExposedWeb Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Imperva
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Abdullah Turkistani
 
C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centre
jatin batra
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
Network Security Fundamentals
Network Security FundamentalsNetwork Security Fundamentals
Network Security Fundamentals
Fat-Thing Gabriel-Culley
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
Vishalini Mugunen
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
Sachin Sharma
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Netw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire classNetw450 advanced network security with lab entire class
Netw450 advanced network security with lab entire class
EugenioBrown1
 
Functions c++ مشروع
Functions c++ مشروعFunctions c++ مشروع
Functions c++ مشروع
ziadalmulla
 
Network-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in PersianNetwork-security muhibullah aman-first edition-in Persian
Network-security muhibullah aman-first edition-in Persian
Muhibullah Aman
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016
Qrator Labs
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Maaz Hasan
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
Ali Aminian
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPP
Wei-Wen Hsu
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You ExposedWeb Applications Under Attack: Why Network Security Solutions Leave You Exposed
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Imperva
 
C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer CentreC++ Programming Language Training in Ambala ! Batra Computer Centre
C++ Programming Language Training in Ambala ! Batra Computer Centre
jatin batra
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Ad

Similar to C++ L05-Functions (20)

C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
WaheedAnwar20
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
temkin abdlkader
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
TarekHemdan3
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
Venkateswarlu Vuggam
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
LokeshK66
 
Oop assignment 02
Oop assignment 02Oop assignment 02
Oop assignment 02
MamoonKhan39
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
yash production
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
Dendi Riadi
 
C++ practical
C++ practicalC++ practical
C++ practical
Rahul juneja
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
kinan keshkeh
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
Abdul Samee
 
Namespaces
NamespacesNamespaces
Namespaces
zindadili
 
Silde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysisSilde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysis
Rayhan331
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
RehmanRasheed3
 
Dd3.15 thru-3.21-advanced-functions
Dd3.15 thru-3.21-advanced-functionsDd3.15 thru-3.21-advanced-functions
Dd3.15 thru-3.21-advanced-functions
temkin abdlkader
 
Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Lec 2.pptx programing errors \basic of programing
Lec 2.pptx programing errors \basic of programingLec 2.pptx programing errors \basic of programing
Lec 2.pptx programing errors \basic of programing
daoodkhan4177
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
TAlha MAlik
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
LokeshK66
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
yash production
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
Dendi Riadi
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
kinan keshkeh
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
Abdul Samee
 
Silde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysisSilde of the cse fundamentals a deep analysis
Silde of the cse fundamentals a deep analysis
Rayhan331
 
Dd3.15 thru-3.21-advanced-functions
Dd3.15 thru-3.21-advanced-functionsDd3.15 thru-3.21-advanced-functions
Dd3.15 thru-3.21-advanced-functions
temkin abdlkader
 
Lec 2.pptx programing errors \basic of programing
Lec 2.pptx programing errors \basic of programingLec 2.pptx programing errors \basic of programing
Lec 2.pptx programing errors \basic of programing
daoodkhan4177
 
Ad

More from Mohammad Shaker (20)

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

Recently uploaded (20)

Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
Routing Riverdale - A New Bus Connection
Routing Riverdale - A New Bus ConnectionRouting Riverdale - A New Bus Connection
Routing Riverdale - A New Bus Connection
jzb7232
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Analog electronic circuits with some imp
Analog electronic circuits with some impAnalog electronic circuits with some imp
Analog electronic circuits with some imp
KarthikTG7
 
How to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdfHow to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdf
jamedlimmk
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
roshinijoga
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Journal of Soft Computing in Civil Engineering
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1
remoteaimms
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Autodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User InterfaceAutodesk Fusion 2025 Tutorial: User Interface
Autodesk Fusion 2025 Tutorial: User Interface
Atif Razi
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
Slide share PPT of NOx control technologies.pptx
Slide share PPT of  NOx control technologies.pptxSlide share PPT of  NOx control technologies.pptx
Slide share PPT of NOx control technologies.pptx
vvsasane
 
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
sss1.pptxsss1.pptxsss1.pptxsss1.pptxsss1.pptx
ajayrm685
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
DED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedungDED KOMINFO detail engginering design gedung
DED KOMINFO detail engginering design gedung
nabilarizqifadhilah1
 
Surveying through global positioning system
Surveying through global positioning systemSurveying through global positioning system
Surveying through global positioning system
opneptune5
 
Routing Riverdale - A New Bus Connection
Routing Riverdale - A New Bus ConnectionRouting Riverdale - A New Bus Connection
Routing Riverdale - A New Bus Connection
jzb7232
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Analog electronic circuits with some imp
Analog electronic circuits with some impAnalog electronic circuits with some imp
Analog electronic circuits with some imp
KarthikTG7
 
How to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdfHow to Buy Snapchat Account A Step-by-Step Guide.pdf
How to Buy Snapchat Account A Step-by-Step Guide.pdf
jamedlimmk
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32Interfacing PMW3901 Optical Flow Sensor with ESP32
Interfacing PMW3901 Optical Flow Sensor with ESP32
CircuitDigest
 
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
Parameter-Efficient Fine-Tuning (PEFT) techniques across language, vision, ge...
roshinijoga
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Control Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptxControl Methods of Noise Pollutions.pptx
Control Methods of Noise Pollutions.pptx
vvsasane
 
Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1Computer Security Fundamentals Chapter 1
Computer Security Fundamentals Chapter 1
remoteaimms
 

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
  翻译: