0% found this document useful (0 votes)
7 views

chapter 05 Functions

Chapter Five of the document discusses functions in programming, defining a function as a collection of statements that perform specific tasks. It covers function formats, variable scope, function prototypes, argument passing methods, default values, function overloading, inline functions, and recursion. Examples are provided to illustrate these concepts, including temperature conversion and factorial calculation.

Uploaded by

Ermias Dejene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

chapter 05 Functions

Chapter Five of the document discusses functions in programming, defining a function as a collection of statements that perform specific tasks. It covers function formats, variable scope, function prototypes, argument passing methods, default values, function overloading, inline functions, and recursion. Examples are provided to illustrate these concepts, including temperature conversion and factorial calculation.

Uploaded by

Ermias Dejene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Fundamentals of Programming

[CoEng1112]

Chapter Five:
Function
Function
• A function is a collection of statements that
performs a specific task.
• structure our programs in a more modular
way

2
5.1 Function format (Type,
Name, Argument, Statement)
• 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 (for example, int x) and which acts
within the function like any other variable.
They 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 latter case it must be delimited by curly
brackets {}.
4
Cont’d
• Example: Program to read the
temperature in Fahrenheit and convert it
into Celsius.

5
#include<iostream.h>
float cel(float x)
{
float p;
p=(5.0 / 9)*(x - 32);
return p;
}
void main()
{
float f, celsius;
cout<<"Enter the temperature in Fahrenheit :";
cin>>f;
celsius=cel(f);
cout<<"The temperature in celsius is: "<<celsius; 6

}
celsius=cel(f);
cout<<"The temperature in celsius is: "<<celsius;
}

7
#include<iostream.h> Enter the temperature
float cel(float x) in Fahrenheit : 32
{ The temperature in
float p; celsius is: 0
p=(5.0 / 9)*(x - 32);
return p;
}
void main()
{
float f, celsius;
cout<<"Enter the temperature
in Fahrenheit :";
cin>>f;
8
Cont’d
• In the main function, when the function is
called, control is lost by main and passed
to function.
celsius=cel(f);
float cel(float x)
• The value returned by a function is the
value given to the function when it is
evaluated.
9
Scope of variables
• 1. Local 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.
• Example:
10
#include<iostream.h>
void function1 ( )
{
int m=10;
cout<<m<<endl;
}
void function2 ( )
{
int m=100;
cout<<m<<endl;
//cout<<n; Creates an error like undefined symbol n
}

11
void main ( )
{
int m=1000,n=999;
function2( );
function1( );
cout<<m<<endl<<n;
}

12
#include<iostream.h> 10
void function1 ( ) 100
{ 1000
int m=10; 999
cout<<m<<endl;
}
void function2 ( )
{
int m=100;
cout<<m<<endl;
//cout<<n; Creates an
error like undefined
symbol n
} 13
void main ( )
{
int m=1000,n=999;
function2( );
function1( );
cout<<m<<endl<<n;
}

14
Cont’d
• 2. Global variables: Global variables are
declared separately, preferably outside the
main function. They are accessible to all
functions included in the program.
• Example:

15
#include<iostream.h>
int y;
void function1 ( )
{
y=y+1;
cout<<y<<endl;
}
void function2 ( )
{
y=y*2;
cout<<y<<endl;
}

16
void main( )
{
y=5;
cout<<y<<endl;
function2( );
function1( );
cout<<y<<endl;
}

17
void main( ) 5
{ 10
y=5; 11
cout<<y<<endl; 11
function2( );
function1( );
cout<<y<<endl;
}

18
Cont’d
• y is defined as global so that function1()
and function2() accessed the variable.

19
Functions with no types -The use
of void
• This kind is peculiar in 2 aspects:
(i) A function with no argument does not
receive data from calling function.
(ii) 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 20
the previous examples.
//Example 2
#include <iostream.h>

void displayMessage ()
{
cout << “Programming is a fun!";
}

int main ()
{
displayMessage ();
return 0;
}
21
//Example 2 Programming is a fun!
#include <iostream.h>

void displayMessage ()
{
cout << “Programming is
a fun!";
}

int main ()
{
displayMessage ();
return 0;
} 22
5.5 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.

23
Cont’d
• Format:
type name ( argument_type1,
argument_type2, ...);

24
#include <iostream.h> Programming is a fun!
void displayMessage ();
int main ()
{
displayMessage ();
return 0;
}
void displayMessage ()
{
cout << “Programming is
a fun!";
}

25
#include <iostream.h> The result is 8
int addition (int a, int b);
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);
}
26
#include <iostream.h> 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);
}
27
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);

28
Arguments passed by value and
by reference
• i) 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.

29
Cont’d
• ii) 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. 30
Cont’d
• the type of each argument was followed by
an ampersand sign (&).
• Example:

31
#include<iostream.h> enter values for p,q,x and
void swap1( int p, int q); y: 3
void swap2( int &x, int &y); 4
void main() 5
{ 6
int x,y,p,q;
cout<<"enter values for
p,q,x and y: ";
cin>>p>>q>>x>>y;
swap1(p,q);
cout<<p<<endl;
cout<<q<<endl;
swap2(x,y);
cout<<x<<endl;
cout<<y<<endl; 32
}
#include<iostream.h> enter values for p,q,x and
void swap1( int p, int q); y: 3
void swap2( int &x, int &y); 4
void main() 5
{ 6
int x,y,p,q; 3
cout<<"enter values for 4
p,q,x and y: "; 6
cin>>p>>q>>x>>y; 5
swap1(p,q);
cout<<p<<endl;
cout<<q<<endl;
swap2(x,y);
cout<<x<<endl;
cout<<y<<endl; 33
}
void swap1(int p, int q)
{
int temp;
temp = p;
p = q;
q = temp;
}
void swap2(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
} 34
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.

35
Cont’d
• 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

36
#include<iostream.h>
float calc(float p, int t=5, float r=10.5)
{
return ( p * t * r / 100);
}
void main()
{
float inter;
inter=calc(50000.00);
cout<<"\n Interest:"<<inter;
inter=calc(50000.00,10);
cout<<"\n Interest:"<<inter;
inter=calc(50000.00,10,12.5);
cout<<"\n Interest:"<<inter;
} 37
#include<iostream.h> Interest:----
float calc(float p, int t=5, float Interest:-----
r=10.5) Interest:-----
{
return ( p * t * r / 100);
}
void main()
{
float inter;
inter=calc(50000.00);
cout<<"\n Interest:"<<inter;
inter=calc(50000.00,10);
cout<<"\n Interest:"<<inter;
inter=calc(50000.00,10,12.5);
cout<<"\n Interest:"<<inter; 38
5.2 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
39
• Examples:
// Example 1
#include <iostream.h>

int divide (int a, int b)


{
return (a/b);
}

float divide (float a, float b)


{
return (a/b);
}

40
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;
}

41
// Example 1 2
#include <iostream.h> 2.5

int divide (int a, int b)


{
return (a/b);
}

float divide (float a, float b)


{
return (a/b);
}

42
//Example 2
#include<iostream.h>
int volume(int s)
{
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);
}

43
void main()
{
cout<<volume(10)<<"\n";
cout<<volume(2.5,8)<<"\n";
cout<<volume(15.5,75,15);
}

44
5.3 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 ... }
45
#include<iostream.h>
inline void area(float x)
{
int a;
a=x*x;
cout<<"area of the square: "<<a;
}
void main()
{
float s;
cout<<" Enter side \n";
cin>>s;
area(s);
} 46
5.4 Recursivity
• Recursivity is the property that functions
have to be called by themselves.
• A recursive function must have an if
statement to check the basic value of the
back substitution. Else it will never return,
goes into the loop of self calling.

47
Cont’d
• For example, to obtain the factorial of a
number (n) its mathematical formula is:
n! = n * (n-1) * (n-2) * (n-3) ... * 1
• More concretely, 5! (factorial of 5) would
be:
5! = 5 * 4 * 3 * 2 * 1 = 120
• A recursive function to do that could be
this:
48
#include <iostream.h> Type a number: 9
long factorial (long a) !9 = 362880
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
int main ()
{
long n;
cout << "Type a number: ";
cin >> n;
cout << "!" << n << " = " <<
factorial (n);
49
return 0;

You might also like