Ch4 - Functions
Ch4 - Functions
Programming
Ch4 – Functions
• Introduction
• Math library functions
• Functions
• By value/ By reference/ By pointer Parameters
Introduction
• A function is a self-contained module that accomplishes a specific task defined outside the main
program (int main())
• It contains a set of instructions that will be executed when the function is called from the main
program.
• A function can be used over and over again (Software reusability)
Ex: suppose a program that handles geometric shapes requires the calculation of the area of a circle 10 times with
different values. Instead of writing the same code 10 times, a function can be created that calculates the area.
• Return-value-type
- Data type of the result returned (int, float, char, bool…)
- Use “void “ if nothing is returned
• Parameter list
- Comma separated list of arguments + Data type for each argument.
- If no arguments, leave blank ()
Functions
void function_name() {
This is a procedure
cout<<“This is a procedure”;
}
A Function that
int main() { return nothing is
... called a
... Procedure
function_name();
...
...
return 0;
}
Functions
void function_name() {
...
...
} Returning type:
What is the data type that the function returns?
• void: it executes the code without returning anything
• int: it executes the code and returns an integer value
upon exiting the function
• float: it executes the code and returns a float value
upon exiting the function
• char: it executes the code and returns a character
value upon exiting the function
• Etc…
Functions
Write a function that prints “Hello World…” on the screen.
void printHello(){
cout<<"Hello World\n"; Hello World
cout<<"I am a procedure\n"; I am a procedure
cout<<"I can be called n times\n\n"; I can be called n times
}
Hello World
I am a procedure
int main(){ I can be called n times
printHello();
printHello(); Hello World
printHello(); I am a procedure
I can be called n times
}
Functions
void function_name(int a) {
...
Arguments:
... • To pass values from the program to a
} function
• A function can have multiple arguments
• Arguments should be of the same type as
int main() { the values to be passed from the program
... into the function
• If there is no arguments () or we can write
...
void.
function_name(2);
...
...
function_name(2.5); // incorrect
return 0;
}
Functions Parameters:
Local variables from the main or
int function_name(int a) { from other functions are passed to
int b; function when called
... Local variables:
... • Known only in the function in which they are
return b; defined
• All variables declared in function definitions
} are local variables
int main(){
return y * y;
int main(){
int nb;
for ( int i=0; i<10; i++){
cout<<“Enter a number:”;
cin>>nb;
cout<< square(nb)<<endl; // calling the function
}
return 0;
Ex1
int main(){
int nb;
for ( int i=0; i<10; i++){
cout<<“Enter a number:”;
cin>>nb;
cout<< square(nb); <<endl;
}
return 0;
}
rand();
Scaling and shifting
Generates unsigned integer between 0 and
RAND_MAX (usually 32767)
x % y is between 0 and y – 1
Example
i = rand() % 6 + 1;
“Rand() % 6”: generates a number
between 0 and 5
“+ 1” makes the range 1 to 6
x=5; Y 1 2
int y, z; Z 2 7
y=2; 3
z=x+y; // z= 5+2 = 7 4
5
return (0);
6
}
Call by value / by reference / By pointer
Parameters
int main(){ Variables Address Memory
… X 0 3
5 x and y variables not modified
x=3; Y 1 2
z= add(x, y); Call by value
Z 2 5
7
- Copy of data passed to
return (0); the function parameters.
3
} - Changes to copy do not
4 change original data
a 5 3
int Add (int a, int b){
return a+b; b 6 2
}
Call by value / by reference / By pointer
Parameters
Variables Address Memory
int main(){
… X 0 23
cout<< x << endl; Call by reference
Y 1 2
changeVar(x) ; - A link is created between
Z 2 5 the original data and the
cout<< x << endl; parameters in the
return (0); 3 function.
} c 4 23 - Any changes in the
parameters leads to a
a 5 3 change in the original
void changeVar (int &c){ data.
b 6 2
c=2;
} 3
2
Call by value / by reference / By pointer
Parameters
int main(){ Variables Address Memory
… X 0 3
2
cout<< x << endl; Call by pointer
Y 1 2
changeVar(&x) ; - Function can directly
// (&x)=(0) Z 2 5
access data.
cout<< x << endl; 3 - Changes affect original
return (0); data.
c 4 0
}
a 5 3
void changeVar (int* c){
*c = 2 ; b 6 2
*(&x)=*(0)=2 3
} 2
Ex5
int main(){ Y 1 24
//Method 1: //Method 2:
float Enter_Grade (){ void Enter_Grade (float &n){
float n; do{
do{ cout<<“Enter grade”;
cout<<“Enter grade”; cin>> n;
cout<< n; }while(n<0 || n>100);
}while(n<0 || n>100); }
return n;
} int main(){
float grade;
int main(){ Enter_Grade (grade);
float grade; return 0;
grade = Enter_Grade(); }
return (0);
}