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

More Related Content

What's hot

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 APISeok-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 PythonHan 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 StudyChris Ohk
 
Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17OdessaFrontend
 
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 LoopsP3 InfoTech Solutions Pvt. Ltd.
 
C tech questions
C tech questionsC tech questions
C tech questionsvijay00791
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTsKevlin Henney
 

What's hot (20)

C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
c programming
c programmingc programming
c programming
 
Unit 3
Unit 3 Unit 3
Unit 3
 
c programming
c programmingc programming
c programming
 
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 ...
 
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...
 
Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency API
 
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...
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
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...
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
C++ L10-Inheritance
C++ L10-InheritanceC++ L10-Inheritance
C++ L10-Inheritance
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17Антихрупкий TypeScript | Odessa Frontend Meetup #17
Антихрупкий TypeScript | Odessa Frontend Meetup #17
 
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
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
C tech questions
C tech questionsC tech questions
C tech questions
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 

Viewers also liked

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 classEugenioBrown1
 
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 PersianMuhibullah Aman
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016Qrator 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++ 3Ali Aminian
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPPWei-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 ExposedImperva
 
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 Centrejatin 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++ PPT03062679929
 

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
 
C++
C++C++
C++
 
Functions c++ مشروع
Functions c++ مشروعFunctions c++ مشروع
Functions c++ مشروع
 
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
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Network Security in 2016
Network Security in 2016Network Security in 2016
Network Security in 2016
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
C++ lecture 03
C++   lecture 03C++   lecture 03
C++ lecture 03
 
The Algebra of Functions
The Algebra of FunctionsThe Algebra of Functions
The Algebra of Functions
 
Learning C++ - Functions in C++ 3
Learning C++ - Functions  in C++ 3Learning C++ - Functions  in C++ 3
Learning C++ - Functions in C++ 3
 
Basic openCV Functions Using CPP
Basic openCV Functions Using CPPBasic openCV Functions Using CPP
Basic openCV Functions Using CPP
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
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
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
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
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Network Security Fundamentals
Network Security FundamentalsNetwork Security Fundamentals
Network Security Fundamentals
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Similar to C++ L05-Functions

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.pdfyamew16788
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functionsTAlha MAlik
 
how to reuse code
how to reuse codehow to reuse code
how to reuse codejleed1
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.pptLokeshK66
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash 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 overloadingkinan keshkeh
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Abdul Samee
 
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-functionstemkin abdlkader
 

Similar to C++ L05-Functions (20)

C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
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
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Cs1123 8 functions
Cs1123 8 functionsCs1123 8 functions
Cs1123 8 functions
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
Ch7 C++
Ch7 C++Ch7 C++
Ch7 C++
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
 
Oop assignment 02
Oop assignment 02Oop assignment 02
Oop assignment 02
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
C++ practical
C++ practicalC++ practical
C++ practical
 
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
 
Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01Part 3-functions1-120315220356-phpapp01
Part 3-functions1-120315220356-phpapp01
 
Namespaces
NamespacesNamespaces
Namespaces
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
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
 
C++ file
C++ fileC++ file
C++ file
 
C++ file
C++ fileC++ file
C++ file
 
Function
FunctionFunction
Function
 

More from 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 GraduateMohammad 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 PsychologyMohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game DevelopmentMohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesMohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - ColorMohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - TypographyMohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingMohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and ThreadingMohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSMohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsMohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsMohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and GamingMohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / ParseMohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesMohammad 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 AdaptersMohammad Shaker
 

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
 
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]
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 

Recently uploaded

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 

Recently uploaded (20)

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 

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