Functions:
Typesof Functions
Function Declaration
Function Definition
Function Call
Parameters
Arguments
Passing Values To functions
Today’s Lecture Agenda
3.
• A functionis a block of code that perform some
actions.
• Functions are used to perform similar action again
and again without writing the same code again.
• The functions provide a structured programming
approach. The whole program logic is divided into
modules.
• Functions only runs when it is called.
• The main function calls these functions when they
are needed to execute.
Functions:
4.
• User DefinedFunction: A type of function written by
programmer is known as user defined function. It has a
unique name. These functions are written according to
the exact need of the user.
• For Example: main() function
• Built In Function: A type of function that is available
as a part of language is known as built-in- function or
library function. These functions are stored in different
header files.
• For Example: clrscr() is a built in function. It is part of
a header file called conio.h
Types of Functions
5.
Three importantaspects for using
functions:
1. Function Definition(code of function):
It is just body of function which is mostly
written below the main function
2. Function declaration
3. Function call
Using Function
6.
• It consistof single statement that inform the compiler about
a function.
• It can be written in the body of main() function.
• Declared functions are not executed immediately. They are
"saved for later use”.
• It is not required if function definition appears before the
main () function.
return_type function_name(parameter_list);
Function Declaration
7.
Function declarationis given if the
function definition exists below the
main() function
Function declaration consists of:
void line();
Function declaration
8.
• Writing thefull body of function is known as defining a
function. It consist of different statements to perform a
particular task.
• It cannot be written in body of main() function.
• It must be written before or after the main() function.
return_type function_name(parameter_list)
{
//Statements inside function
}
Function Definition
9.
return-value-type function-name (parameter-list)
{
declarationsand statements
}
Return-Type: No(void) return or type
Function-name: Any(identifier rules)
Parameter: No(void) or no. of parameters with types
Body of the function
User-defined Functions
(Definition)
10.
void line( ){
for(inti=0;i<45;i++)
cout<<“*”;
cout<<endl;
}
Simple Function (Function
Definition)
No Return Name No Parameter
11.
• The statementthat activates a function is known as function
call. A function is called with its name.
• When a function is called, the following steps take place:
The control moves to the function that is called.
All statements in the function body are executed.
The control return back to the calling function.
The remaining statements in the calling function are
executed.
To call a function, write the function's name followed by
two parentheses () and a semicolon ;
Foe Example: sum(1,99);
Function Call
12.
• The parameteris referred to as the variables that are defined
during a function declaration or definition.
• Parameters are given in parenthesis. If there are many
parameters these are separated by commas. If there is no
parameter, Process of passing parameter empty parenthesis are
used.
• Parameters in function call are called actual parameters or
arguments.
• Parameters in function declaration are called formal
parameters.
Parameters
13.
Formal parameters arewritten in the
function prototype and function header of
the definition.
Formal parameters are local variables which are
assigned values from the arguments when the
function is called.
Formal Parameters
When afunction is called, the values
(expressions) that are passed in the call are called
the arguments or actual parameters (both terms mean
the same thing).
The time of the call each actual parameter is
assigned to the corresponding formal parameter
in the function definition.
Actual Parameters
• An argumentis referred to the values that are passed
within a function when the function is called.
• These values are assigned to the variables in the definition
of the function that is called.
Arguments
19.
A functionis executed only when it
is called
Mostly a function is called inside the
main (or in other function)
Code written inside function will
never execute if it is not called
For example: line();
Function call
20.
void line(); Function Declaration
using namespace std;
int main ( ) {
line( ); Function Call
cout<<"Data Type Memory Reservedn";
line(); Function Call
cout<<"bool 1 Bit"<<endl
<<"char 1 Byte"<<endl
<<"int 4 Byts"<<endl;
line( ); Function Call
return 0;
}
void line( ){
for(int i=0;i<40;i++)
cout<<"*"
cout<<endl;
}
Using Function
Function
Definition
Write a functionwhich input 5 integers and calculate average
Example
• #include <iostream>
• void namespace std;
• void average();
• int main()
• {
• average();
• return 0;
• }
• void average(){
• int a,b,c,d,e;
• cout<<"n Enter Five Numbers:";
• cin>>a>>b>>c>>d>>e;
• cout<<endl<<(a+b+c+d+e)/5.0;
• }
24.
Write a functionwhich input an integer number and tells whether it is
even or odd (using switch).
Example :
• #include <iostream>
• void evenOdd();
• using namespace std;
• int main() {
• evenOdd();
• return 0;
• }
• void evenOdd(){
• int number;
• cout<<"n Enter a Number:";
• cin>>number;
• switch(number%2){
• case 0: cout<<"nNumber is even"; break;
• case 1: cout<<"n Number is odd";
• }
• }
25.
Call byvalue
Call by Reference
A function can be invoked in 2 ways
26.
A copyof the actual parameter is passed to the function.
Any modification inside the function does not affect the original
variable.
This is the default behavior in C++.A copy of the actual
parameter is passed to the function.
Any modification inside the function does not affect the original
variable.
This is the default behavior in C++.
Call by value
28.
Example of callby value
Formal
Parameter
a
value
copied
sid
e Actual parameter
#include<iostream.h>
Int cube (int);
int main( )
{
int vol, side=7;
vol=cube(side);
cout<<vol; Return 0;
}
int cube(int a)
{ return a*a*a*;
}
7
7
29.
A parameterpassing mechanism in which the value of
actual parameter is copied to formal parameters of
called function is known as pass by value. If the
function makes any change in formal parameters it
does not affect the values of actual parameter. It is the
default mechanism for passing parameters to functions.
Passing Values To functions
30.
void add(inta, int b); OR void add (int, int);
int main(){
add (7,8);
add (9,10);
int x=5, y=10;
add (x,y);
return 0;
}
Passing Values to a Function
• void add( int a, int b){
• int add;
• add= a+b;
• cout<<“ Addition is: “<<add<<endl;
• }
34.
Write a functionwhich takes three integers as
parameters and display the smallest integer.
Problem