Chapter 4 Computer Programmingodp
Chapter 4 Computer Programmingodp
[ECEg-1052]
Chapter Four:
Function
Function format:
type name ( argument1, argument2, ...)
statement
where:
Type is the type of data returned by the function.
Name is the name by which it will be possible to call the function.
Arguments allow passing parameters to the function when it is called. 3
Cont’d...
Each argument consists of a type of data followed by its identifier, like
in a variable declaration and which acts within the function like any other
variable.
The different parameters are separated by commas.
Statement is the function's body.
It can be a single instruction or
a block of instructions.
• In the this case it must be delimited by curly brackets { }.
4
Cont’d...
Example: Program to read the temperature in Fahrenheit and convert it into
Celsius.
#include<iostream> Output:
using namespace std; Enter the temperature in Fahrenheit :
float cel(float x){
float p; 32
p=(5 / 9)*(x - 32);
The temperature in celsius is:
return p;
} 0
int main(){
float f, celsius;
cout<<"Enter the temperature in Fahrenheit :";
cin>>f;
celsius=cel(f);
cout<<"The temperature in celsius is:"<<celsius;
}
Cont’d...
In the main function, when the function is called, control is lost by main
and passed to called function.
celsius=cel(f);
The value returned by a function is the value given to the function when it
is evaluated.
6
Cont’d...
Scope of variables
Variables declared within a function are called as local variables.
They are created when the function is called and destroyed
automatically when the function is exited.
Scope of the local variable is with in the function only; it is not valid out
side of the function.
7
Cont’d...
Example
#include<iostream> Output
using namespace std; 10
void function1 ( ){
int m=10; // Local variable 100
cout<<m<<endl; 1000
} 999
void function2 ( ){
int m=100; // Local variable
cout<<m<<endl;
}
int main ( ){
int m=1000 , n=999; // Local variable
function1( );
function2( );
cout<<m<<endl<<n;
} 8
Cont’d…
Global variables
Global variables are declared separately, preferably outside the main
function.
They are accessible to all functions included in the program.
9
Cont’d…
Example:
#include<iostream> y is defined as global so that
using namespace std; function1() and function2() accessed
int y; // Global variables
void function1 ( ){
the variable.
y=y+1;
cout<<y<<endl;
Output
} 5
void function2 ( ){ 10
y=y*2;
cout<<y<<endl; 11
} 11
int main( ){
y=5;
cout<<y<<endl;
function2( );
function1( );
cout<<y<<endl;
} 10
Functions with no types -The use of void
This kind is peculiar in 2 aspects:
➔ A function with no argument does not receive data from calling function.
➔ A function with no return value, does not give any data to calling
function.
Thus the function can be used as an independent statement but not as an
expression.
Example 1: function1() and function2() in the previous examples.
11
Cont’d…
//Example 2 Output
#include<iostream> Programming is a fun!
using namespace std;
void displayMessage (){
cout << "Programming is a fun!";
}
int main (){
displayMessage ();
return 0;
}
12
Prototyping functions
To call a function it must have been declared previously (it must be known).
There is an alternative way to avoid writing all the code of all functions before
they can be used in main or in another function.
➔
It is by prototyping functions.
A function prototype eliminates the need to place a function definition before
all calls to the function.
Format:
type name ( argument_type1, argument_type2, ...);
13
Cont’d...
#include<iostream> Output
using namespace std; Programming is a fun!
void displayMessage();
int main (){
displayMessage ();
return 0;
}
void displayMessage (){
cout << "Programming is a fun!";
}
14
Cont’d...
#include <iostream> Output
using namespace std;
int addition (int a, int b); The result is 8
int main (){
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
int addition (int a, int b){
int r;
r=a+b;
return (r);
}
15
Cont’d…
The inclusion of a name for each argument as in the definition of a
standard function is optional, although recommended.
Example:
int addition (int , int);
16
Cont’d...
#include <iostream> Output
using namespace std; The result is 8
int addition (int, int );
int main (){
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
int addition (int a, int b){
int r;
r=a+b;
return (r);
}
17
Arguments passed by value and by reference
Passed by Value
This means that when calling a function with parameters, what we have passed to
the function were values but never the specified variables.
This is one way of data transformation from calling portion to called portion.
➔
Means changes inside the function cannot affect the main program.
18
Cont’d...
Passed by reference
This is two way of data transformation from calling portion to called
portion and called portion to calling portion.
➔Means when function is called by reference, changes inside the function
affect main program also.
When passing a variable by reference we are passing the variable itself
and any modification that we do to that parameter within the function will
have effect in the passed variable outside it.
This type of each argument was followed by an ampersand sign (&).
19
Cont’d...
Example: void swap1(int p, int q){
#include <iostream> int temp;
using namespace std; temp = p;
void swap1( int p, int q); p = q;
void swap2(int &x, int &y); q = temp;}
int main(){ void swap2(int &x, int &y){
int x,y,p,q; int temp;
cout<<"Enter values for p,q,x and y: "; temp = x;
x = y; Enter values for p,q,x
cin>>p>>q>>x>>y; and y:
swap1(p,q); y = temp;}
3
cout<<p<<endl; 4
cout<<q<<endl; 5
swap2(x,y); 6
cout<<x<<endl; 3
cout<<y<<endl; 4
} 6
5 20
Default values in arguments
When declaring a function we can specify a default value for each
parameter.
This value will be used if that parameter is left blank when calling to the
function.
We must add default values from right to left only.
Examples:
int mul(int i, int j=5, int k=10); //legal
int mul(int i=2,int j=5, int k=10); //legal
Illegal examples:
int mul(int i=5,int j); //illegal
int mul(int i=0, int j, int k=10); //illegal 21
Cont’d...
#include <iostream>
Output
using namespace std;
float calc(float p, int t=5, float r=10.5){
Interest: 262.5
return ( p * t * r / 100); Interest: 525
} Interest: 625
int main(){
float inter;
inter=calc(500.00);
cout<<"\n Interest: "<<inter;
inter=calc(500.00,10);
cout<<"\n Interest: "<<inter;
inter=calc(500.00,10,12.5);
cout<<"\n Interest: "<<inter;
}
22
Overloaded functions
Uses the same function name for more than one function.
The functions must have something different about their arguments, either
a different number of arguments or arguments of different types.
The compiler uses the differences in the argument list to distinguish one
function from another.
23
Cont’d...
// Example 1: Arguments of different types
#include <iostream> Output
using namespace std; 2
int divide (int a, int b){
return (a/b); 2.5
}
float divide (float a, float b){
return (a/b);
}
int main (){
int x=5,y=2;
float n=5.0, m=2.0;
cout << divide (x,y);
cout << "\n";
cout << divide (n,m);
cout << "\n";
return 0;
} 24
Cont’d...
//Example 2
Output
//number of arguments
#include <iostream>
1000
using namespace std; 157.075
int volume(int s){ 17437.5
return(s*s*s);}
double volume(double r, int h){
return(3.1415*r*r*h);}
float volume(float l, int b, int h){
return(l*b*h);}
int main(){
cout<<volume(10)<<"\n";
cout<<volume(2.5,8)<<"\n";
cout<<volume(15.5,75,15);
} 25
Inline functions
C++ provides an inline function where compiler writes the code for the
function directly in place where it is invoked, rather than generating target
function for a function & invoking every time when needed.
Format:
inline type name ( arguments ... ) { instructions ... }
28
Cont’d...
For example, to obtain the factorial of a number (n) its mathematical
formula is:
5! = 5 * 4 * 3 * 2 * 1 = 120
A recursive function to do that could be this:
29
Cont’d...
#include <iostream> Output
using namespace std;
long factorial (long a){ Type a number: 9
if (a > 1) !9 = 362880
return (a * factorial (a-1));
else
Return (1);
}
int main (){
long n;
cout << "Type a number: ";
cin >> n;
cout << "!" << n << " = " << factorial (n);
Return 0;
} 30
Thank You !
31