4. Functions
•Two ways to write functions in your program:
•Declaration / calling / definition
5. Functions
•Two ways to write functions in your program:
•Declaration / calling / definition
Before main
Inside main
After main
6. Functions
•Two ways to write functions in your program:
•Declaration / calling / definition
•Declaration + definition / calling
7. Functions
•Declaration / calling / definition
#include<iostream>
using namespace std ;
Float avg(float x, float y ) ;
int main ()
{
float x,y;
cout<<" Enter the first number: n";
cin>>x;
cout<<" Enter the second number: n";
cin>>y;
float c=avg(x,y);
cout<<"the avg : "<<c<<endl;
system("pause");
return 0;
}
float avg(float x, float y )
{
return (x+y)/2;
}
8. #include<iostream>
using namespace std ;
Float avg(float x, float y ) ;
int main ()
{
float x,y;
cout<<" Enter the first number: n";
cin>>x;
cout<<" Enter the second number: n";
cin>>y;
float c=avg(x,y);
cout<<"the avg : "<<c<<endl;
system("pause");
return 0;
}
float avg(float x, float y )
{
return (x+y)/2;
}
Functions
•Declaration / calling / definition
Declaration
9. #include<iostream> using namespace std ; Float avg(float x, float y ) ; int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avg : "<<c<<endl; system("pause"); return 0; } float avg(float x, float y ) { return (x+y)/2; }
Functions
•Declaration / calling / definition
Declaration
calling
10. #include<iostream>
using namespace std ;
Float avg(float x, float y ) ;
int main ()
{
float x,y;
cout<<" Enter the first number: n";
cin>>x;
cout<<" Enter the second number: n";
cin>>y;
float c=avg(x,y);
cout<<"the avg : "<<c<<endl;
system("pause");
return 0;
}
float avg(float x, float y )
{
return (x+y)/2;
}
Functions
•Declaration / calling / definition
Declaration
calling
Definition
11. Functions
•Declaration + definition / calling
#include<iostream> using namespace std ; float avg(float x, float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; }
12. Functions
•Declaration + definition / calling
#include<iostream> using namespace std ; float avg(float x, float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; }
Declaration+ definition
13. Functions
•Declaration + definition / calling
#include<iostream> using namespace std ; float avg(float x, float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; }
Declaration+ definition
calling
14. Functions
#include<iostream> using namespace std ; float avg(float x , float y ) { return (x+y)/2; } int main () { float x,y; cout<<" Enter the first number: n"; cin>>x; cout<<" Enter the second number: n"; cin>>y; float c=avg(x,y); cout<<"the avssg : "<<c<<endl; system("pause"); return 0; }
Returned value
15. Functions
#include<iostream>
using namespace std ;
float avg(float x , float y )
{
return (x+y)/2;
}
int main ()
{
float x,y;
cout<<" Enter the first number: n";
cin>>x;
cout<<" Enter the second number: n";
cin>>y;
float c=avg(x,y);
cout<<"the avssg : "<<c<<endl;
system("pause");
return 0;
}
It could be Void , then no return and no “ = ”
16. Functions
#include<iostream>
using namespace std ;
float avg(float x , float y )
{
return (x+y)/2;
}
int main ()
{
float x,y;
cout<<" Enter the first number: n";
cin>>x;
cout<<" Enter the second number: n";
cin>>y;
float c=avg(x,y);
cout<<"the avssg : "<<c<<endl;
system("pause");
return 0;
}
Attention !!
“ , ” Not “ ; ”
17. Functions
#include<iostream>
using namespace std ;
float avg(float x , float y )
{
return (x+y)/2;
}
int main ()
{
float x,y;
cout<<" Enter the first number: n";
cin>>x;
cout<<" Enter the second number: n";
cin>>y;
float c=avg(x,y);
cout<<"the avssg : "<<c<<endl;
system("pause");
return 0;
}
Variables like in procedures & functions in Pascal !
18. Functions
#include<iostream>
using namespace std ;
float avg(float x , float y )
{
return (x+y)/2;
}
int main ()
{
float x,y;
cout<<" Enter the first number: n";
cin>>x;
cout<<" Enter the second number: n";
cin>>y;
float c=avg(x,y);
cout<<"the avssg : "<<c<<endl;
system("pause");
return 0;
}
Also , it could be null
Float avg()
20. Predefined Functions
•It’s existing in libraries in C++
•You can call it without writing any definition or declaration !!
•You have to put using namespace std with all functions !
21. Predefined Functions
Name
Description
Type of
Arguments
Type of
Value
Example
Value
Library
Header
sqrt
Square root
double
double
sqrt(4.0)
2.0
cmath
pow
Powers
double
double
pow(2.0,3.0)
8.0
cmath
abs
Absolute value for int
int
int
abs(-7)
abs(7)
7
7
cstdlib
labs
Absolute value for long
long
long
labs(-70000) labs(70000)
70000 70000
cstdlib
fabs
Absolute value for double
double
double
fabs(-7.5) fabs(7.5)
7.5
7.5
cmath
ceil
Ceiling (round up)
double
double
ceil(3.2) ceil(3.9)
4.0
4.0
cmath
floor
Floor (round down)
double
double
floor(3.2) floor(3.9)
3.0
3.0
cmath
exit
End program
int
void
exit(1);
None
cstdlib
rand
Random number
None
int
rand( )
Varies
cstdlib
srand
Set seed for rand
unsigned int
void
srand(42);
None
cstdlib
22. Predefined Functions
Name
Description
Type of
Arguments
Type of
Value
Example
Value
Library
Header
sqrt
Square root
double
double
sqrt(4.0)
2.0
cmath
pow
Powers
double
double
pow(2.0,3.0)
8.0
cmath
abs
Absolute value for int
int
int
abs (-7)
abs(7)
7
7
cstdlib
labs
Absolute value for long
long
long
labs(-70000) labs(70000)
70000 70000
cstdlib
fabs
Absolute value for double
double
double
fabs(-7.5) fabs(7.5)
7.5
7.5
cmath
ceil
Ceiling (round up)
double
double
ceil(3.2) ceil(3.9)
4.0
4.0
cmath
floor
Floor (round down)
double
double
floor(3.2) floor(3.9)
3.0
3.0
cmath
exit
End program
int
void
exit(1);
None
cstdlib
rand
Random number
None
int
rand( )
Varies
cstdlib
srand
Set seed for rand
unsigned int
void
srand (42);
None
cstdlib
23. ///Computes the size of a doghouse that can be purchased //given the user’s budget. #include <iostream> #include <cmath> using namespace std; int main( ){ const double COST_PER_SQ_FT = 10.50; //cost per square feet double budget, area, lengthSide; cout << "Enter the amount budgeted for your doghouse $"; cin >> budget; area = budget/COST_PER_SQ_FT; lengthSide = sqrt(area); cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << "For a price of $" << budget << endl << "I can build you a luxurious square doghousen" << "that is " << lengthSide << " feet on each side.n"; return 0; }
Predefined Functions
24. #include <iostream>
#include <cstdlib>
using namespace std;
int main( ){
int month, day;
cout << "Welcome to your friendly weather program.n"
<< "Enter today’s date as two integers for the month and the day:n";
cin >> month;
cin >> day;
srand(month*day); //using seed number=month*day
int prediction;
char ans;
cout << "Weather for today:n";
do{
prediction = rand( ) % 3 ;// Scaling
switch (prediction){
case 0:
cout << "The day will be sunny!!n";
break;
case 1:
cout << "The day will be cloudy.n";
break;
case 2:
cout << "The day will be stormy!n";
break;
default:
cout << "Weather program is not functioning properly.n";
}
cout << "Want the weather for the next day?(y/n): ";
cin >> ans;
} while (ans == 'y' || ans == 'Y');
cout << "That's it from your 24-hour weather program.n";
Return 0 ;
}
Code discussion 3
27. Local & global Variables
•Local Variables
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ .
•You can use the same name of variables in different scopes !
#include<iostream>
using namespace std ;
int main (){
float x;
cout<<" Enter the number: n";
cin>>x;
{
float x=22;
}
cout<<"the number x : "<<x<<endl;
system("pause"); return 0;
}
Block!
28. Local & global Variables
•Local Variables
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ .
•You can use the same name of variables in different scopes !
#include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
29. Local & global Variables
•Local Variables
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ .
•You can use the same name of variables in different scopes !
#include<iostream>
using namespace std ;
int main (){
float x;
cout<<" Enter the number: n";
cin>>x;
{
float x=22;
}
cout<<"the number x : "<<x<<endl;
system("pause"); return 0;
}
30. Local & global Variables
•Local Variables
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ .
•You can use the same name of variables in different scopes !
#include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
31. Local & global Variables
•Local Variables
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ .
•You can use the same name of variables in different scopes !
#include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
32. Local & global Variables
•Local Variables
•It’s scope is the current ‘Block’ and all the ‘nested- blocks’ .
•You can use the same name of variables in different scopes !
#include<iostream> using namespace std ; int main (){ float x; cout<<" Enter the number: n"; cin>>x; { float x=22; } cout<<"the number x : "<<x<<endl; system("pause"); return 0; }
34. Local & global Variables
•Global Variables
•At the first of program ( after “using namespace std” )
•It’s scope , all ‘blocks’
•If you have global variable (name x ) and other local variable ( name x too )
•If you want global x , you will write ::x
48. Overloading
•The same functions names ,but different meanings
•Returned_value Fname ( int v1 , double v1 …. )
•Returned_value Fname ( double v1 , double v1 …. )
•Returned_value Fname ( char v1 , double v1 …. )
Difference here
At parameters !
49. Overloading
•The same functions names ,but different meanings
•Returned_value Fname ( int v1 , double v1 …. )
•Returned_value Fname ( double v1 , double v1 …. )
•Returned_value Fname ( char v1 , double v1 …. )
Difference here At parameters !
In numbers of them , and in their kinds
50. Overloading
•The same functions names ,but different meanings
•Returned_value Fname ( int v1 , double v1 …. )
•Returned_value Fname ( double v1 , double v1 …. )
•Returned_value Fname ( char v1 , double v1 …. )
Difference here At parameters !
In numbers of them , and in their kinds
Not here at returned value !
51. Overloading
•The same functions names ,but different meanings
•Returned_value Fname ( int v1 , double v1 …. )
•Returned_value Fname ( double v1 , double v1 …. )
•Returned_value Fname ( char v1 , double v1 …. )
•You will see it on operators + , - , * ….
57. Homework
•Write a program that calls a function , in function the user input a Float number (ex: 3.6) , then the output is “ (3) and (0.6) “
•Write a program that calls a Recursive function , in function the user input an integer number (ex: 3) , then the output is “ (3! = 6) “