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

L5 - Function

Uploaded by

vevele3809
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

L5 - Function

Uploaded by

vevele3809
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CE 204: Computer Programming Sessional

Function
1
Ahmed Farhan Ahnaf Siddique

Assistant Professor,
Department of Civil Engineering,
BUET

2
Function
• A function is a named block of code

• To make large programs manageable, programmers


modularize them into subprograms

• These subprograms are called functions


• They can be compiled and tested separately and
reused in different programs

• All C++ programs must have at least one function


named main()

• The body of a function is enclosed in { }


3
Function
• The function name has a type e.g.

int main() {}

• When a function name is encountered (in other


words when the function is called) execution of the
program branches to the body of that function

• When the function returns, execution resumes on


the next line of the calling function

• You must declare a function before you use it

4
User Defined Function
#include <iostream>
using namespace std;
int cube(int x)
{ // returns cube of x:
return x*x*x;
}
int main()
{ // tests the cube() function:
int n=1;
while (n != 0)
{cin >> n;
cout << "\tcube(" << n << ") = " << cube(n) << endl;
}
} 5
User Defined Function
• A user-defined function has two parts: its head and
its body; The syntax for the head is,

return-type name(parameter-list)

• This specifies for the compiler the function’s return


type, its name, and its parameter list

• The body of a function is the block of code that


follows its head containing the code that performs
the function’s action, including the return
statement that specifies the value that the function
sends back to the place where it was called 6
User Defined Function
• A function’s return statement serves two purposes:
it terminates the execution of the function, and it
returns a value to the calling program

• Its syntax is,


return expression;

• Expression is any expression whose value could be


assigned to a variable whose type is the same as the
function’s return type

7
Function

• A function can be declared in three ways:

i. write the definition of a function before it is called

ii. write a prototype into the file in which your


function is used

iii. write a prototype into a file and then use the


#include directive to include it in your program

user defined functions er jonno, #include" header_file_name.h" use kora hoy,


standard functions er jonno zekhane <...> use kora hoto

8
Function
• The declaration tells the compiler the name, return
type and parameters of the function

• The definition tells the compiler how the function


works

• A function declaration is simply the function’s


head, followed by a semicolon

• A function definition is the complete function:


header and body

• A function declaration is also called a function


prototype
9
Function
• A function declaration is like a variable declaration;
its purpose is simply to provide the compiler with
all the information it needs to compile the rest of
the file

• The compiler does not need to know how the


function works (its body)

• It only needs to know the function’s name, the


number and types of its parameters, and its return
type

10
Function

• This is precisely the information contained in the


function’s head

• Also like a variable declaration, a function


declaration must appear above any use of the
function’s name

• The function definition, when listed separately


from the declaration, may appear anywhere outside
the main() function and is usually listed after it or
in a separate file

11
Function
• The variables that are listed in the function’s
parameter list are called parameters

• They are local variables that exist only during the


execution of the function

• Their listing in the parameter list constitutes their


declaration

• The variables that are listed in the function’s calls


are called the arguments

• Like any other variable in the main program, they


must be declared before they are used in the call 12
Function Example - 1
#include <iostream>
using namespace std;
int circle()
{
double radius;
cout<<"\n Radius = ?";
cin>>radius;
cout<<"\n Area = "<<3.14159*radius*radius<<endl<<endl;
}
int rectangle()
{
double length,width;
cout<<"\n Length = ? ";
cin>>length;
cout<<" Width = ? ";
cin>>width;
cout<<"\n Area = "<<length*width<<endl<<endl;
}
13
Function Example - 1
int main()
{
int option;
cout<<" Program to calculate area \n"
<<" 1 - Circle \n"
<<" 2 - Rectangle \n\n";
cout<<" What option = ? ";
cin>>option;
if(option==1)circle();
if(option==2)rectangle();
}

14
Function Example - 2
#include <iostream>
using namespace std;
int circle();
int rectangle();
int main()
{
int option;
cout<<" Program to calculate area \n"<<" 1-Circle
\n"<<" 2-Rectangle \n\n";
cout<<" What option = ? ";
cin>>option;
if(option==1)circle();
if(option==2)rectangle();
} 15
Function Example - 2
int circle()
{
double radius;
cout<<"\n Radius = ? ";
cin>>radius;
cout<<"\n Area = "<<3.14159*radius*radius<<endl
<<endl;
}
int rectangle()
{
double length, width;
cout<<"\n Length = ? ";
cin>>length;
cout<<" Width = ? ";
cin>>width;
cout<<"\n Area = "<<length*width<<endl<<endl;
} 16
Function Example - 3
#include <iostream>
using namespace std;
double circle(double radius);
double rectangle(double length, double radius);
int main()
{
int option;
cout<<" Program to calculate area \n"<<" 1-Circle \n"<<" 2 -
Rectangle \n\n";
cout<<" What option = ? ";
cin>>option;
if(option==1)
{
double radius;
cout<<"\n Radius = ? ";
cin>>radius;
cout<<"\n Area = "<<circle(radius)<<endl<<endl;
17
}
Function Example - 3
if(option==2)
{
double length,width;
cout<<"\n Length = ? ";
cin>>length;
cout<<" Width = ? ";
cin>>width;
cout<<"\n Area = "<<rectangle(length, width) <<endl<<endl;
}
}
double circle(double radius)
{
return 3.14159*radius*radius;
}
double rectangle(double length, double width)
{
return length*width;
18
}
Pass By Value and Pass By Reference
• Pass by value means that the expression used in the
function call is evaluated first and then the
resulting value is assigned to the corresponding
parameter in the function’s parameter list before
the function begins executing

• However, there are some situations where a


function needs to change the value of the
parameter passed to it

• That can be done by passing it by reference


19
Pass By Value and Pass By Reference
• To pass a parameter by reference instead of by
value, simply append an ampersand, &, to the type
specifier in the function’s parameter list

• This makes the local variable a reference to the


argument passed to it

• So, the argument is read-write instead of read-only

• Then, any changes made to the local variable inside


the function will cause the same changes to the
argument that was passed to it

20
Pass By Value and Pass By Reference

• Note that parameters that are passed by value are


called value parameters, and parameters that are
passed by reference are called reference parameters

21
Pass By Value
#include <iostream>
using namespace std;
int swap(int n1, int n2);
int main()
{
int x,y;
cout<<"\n x = ? "; cin>>x;
cout<<"\n y = ? "; cin>>y;
cout<<"\n\n main - after input : x = "<<x<<"\ty =
"<<y;
swap(x, y); 22
Pass By Value
cout<<"\n\n main - after calling swap : x =
"<<x<<"\ty = "<<y<<endl<<endl;
}
int swap(int x, int y)
{
int z;
z=x; x=y; y=z;
cout<<"\n\n swap - after interchange : x =
"<<x<<"\ty = "<<y;
return 0;
} 23
Pass By Value

24
Pass By Reference
#include <iostream>
using namespace std;
int swap(int& n1, int& n2);
int main()
{
int x,y;
cout<<"\n x = ? "; cin>>x;
cout<<"\n y = ? "; cin>>y;
cout<<"\n\n main - after input : x = "<<x<<"\ty =
"<<y;
swap(x, y); 25
Pass By Reference
cout<<"\n\n main - after calling swap : x =
"<<x<<"\ty = "<<y<<endl<<endl;
}
int swap(int& x, int& y){
int z;
z=x; x=y ; y=z;
cout<<"\n\n swap - after interchange : x =
"<<x<<"\ty = "<<y;
return 0;
}

26
Pass By Reference

27
The inline Function
Inline functions in C++ are a performance optimization technique that suggests to the compiler to substitute the
function's code directly at the point of call, rather than generating a separate function call instruction.
• When a function is called, it takes a lot of extra time
in executing a series of instructions for tasks such as
jumping to the function, saving registrars, pushing
arguments into the stack and returning to the
calling function

• When a function is small, a substantial percentage


of execution time may be spent in such overhead

• To eliminate the cost of calls to such small


functions, C++ has a feature called inline function

28
The inline Function

• The compiler replaces the function call with the


corresponding function code

inline function-header

{
inline int add(int a, int b) {
return a + b;
function body }

29
Function Overloading/Polymorphism
• C++ allows you to use the same name for different
functions

• As long as they have different parameter type lists,


the compiler will regard them as different functions

• To be distinguished, the parameter lists must either


contain a different number of parameters, or there
must be at least one position in their parameter lists
where the types are different

30
Function Overloading/Polymorphism
#include <iostream>
using namespace std;
double x(int);
double x(double);
double x(int, int);
double x(int, double);
double x(double, int);
int main( )
{
cout<<"\n\n x(int 2) called-returned "<<x(2);
cout<<"\n\n x(double 2.0) called-returned "<<x(2.0);
cout<<"\n\n x(int 2, int 3) called-returned "<<x(2, 3);
cout<<"\n\n x(int 2, double 3.0) called-returned "<<x(2, 3.0);
cout<<"\n\n x(double 2.0, int 3) called-returned "<<x(2.0,3)
<<"\n\n";
} 31
Function Overloading/Polymorphism
double x(int y)
{return y;}
double x(double y)
{return y+1;}
double x(int y, int z)
{return y+z;}
double x(int y, double z)
{return y+z+1;}
double x(double y, int z)
{return y+z+2;}

32
Function Overloading/Polymorphism

33
Thank You!
34

You might also like