Week2 Functions
Week2 Functions
Module 2: FUNCTIONS
FUNCTIONS
Void main() Void main()
{ {
Statement 1; Statement 1;
Statement 2; Statement 2;
Statement 3; Sum1();
. Statement 3;
. Statement 4;
. Sum2();
. Statement 5;
Statement n; Statement 6;
} }
Advantages
• Support for modular programming
• Reduction in program size.
• Code duplication is avoided.
• Code reusability is provided.
• Functions can be called repetitively.
• A set of functions can be used to form libraries.
Types
1.Built in functions :-
are part of compiler package.
Part of standard library made available by compiler.
Can be used in any program by including respective header file.
multiply()
{
;
}
Parts of a function
main function
{
function prototype declaration
function call
-------;
}
function declaratory/definition
{
------;
return statement
}
Function prototype
A function prototype is a declaration of a function that tells the program about
the type of value returned by the function, name of function, number and type
of arguments.
Syntax: Return_type function_name (parameter list/argument);
int add(int,int);
void add(void);
int add(float,int);
add(x,y);
add(40,60);
add(void); or add();
Note: data type not to be mentioned.
int float
Now suppose the function return some integer value, you can use a
variable to store that value.
i.e z=add(x,y);
Parts of a function
main function
{
function prototype declaration
function call
}
function declaratory/definition
{
return statement
}
Function definition 2 parts
Syntax: function_type function_name(parameter Function
list) { header
Note: no semicolon in
Local variable declaration; header
Function
Function body statement;
body
Return statement;
}
Example: int add(int,int); //prototype
Z=add(x,y); //function call
void
sqr()
{
int no;
cout<<“enter a no.”;
cin>>no;
cout<<“square
of”<<no<<“is”<<no
*no;
ii. Function will not return any value but passes argument
#include<iostream.h>
#include<conio.h>
void add(int,int);
int main()
{
int a,b;
add(a,b);
cout<<“enter values of a and b”<<endl;
cin>>a>>b;
add(a,b);
a
getch();
return 0; b
}
void
add(int
x,int y)
void add(int x,int y);
{
int c;
c=x+y;
cout<<
iii) Function with arguments and return value
main function
{
int sqr(int); //function prototype
int a,ans;
cout<<“enter a number”;
cin>>a;
ans=sqr(a); //function call
cout<<“square of number is”<<ans;
getch();
return 0;
}
int a=100;
Pointer stores
1)a name this memory
location, no
direct value is
100 2)value stored.
3)address
a
Declaration: Data_type *variable_name;
100
int a=100; address 1
int *ptr;
Intialisation: ptr=&a; //
“Address of” operator referencin
ptr
g
or address 1
int a=100;
Pointer initialisation
int *ptr=&a; address 2
and declaration
Note: a pointer can be used to point to another pointer also i.e it can
store the address of another pointer.
a
100
int a=100; address 1 cout<<a; //100
int *ptr=&a; cout<<*ptr;//100
ptr cout<< **ptr1; //100
int **ptr1=&ptr;
address 1
address 2
Note: pointer 1 points to address of ptr.
ptr1
address 2
address 3
#include<iosream.h> Chain of pointers
#include<conio.h> p1 p2
a
int main()
100 address 1 address 2
{
int a=100; address 1 address 2 address 3
int *p1;
int * *p2;
p1=&a; //p1 points to address of a
p2=&p1; // p2 points to address of p1
cout<<“address of a”<<&a;
cout<<“address of a”<<p1;
cout<<“value of a”<< *p1;
cout<<“value of a”<< * *p2;
cout<<p2;
cout<< *p2;
getch();
}
Reference variable in C++
When a variable is declared as reference, it becomes an alternative name
for an existing variable. A variable can be declared as reference by
putting ‘&’ in the declaration.
int a=100;
Now we will use a reference variable i.e two names
for same memory location.
a
int a=100;
100 int &ref=a; //initialization and declaration
C=a+b; //same
output C=ref+b;
Program using reference variable
#include<iostream.h>
#include<conio.h>
int main() 100
{ 100
int a=100; 0012dcD2
int &ref=a; 0012dcD2
cout<<“va
lue of a
is”<<a; Both “a” and “ref” are used for
same memory location as
cout<<“va alternative name
lue of ref
is”<<ref;
cout<<address of a is”<<&a; P.P.Krishnaraj
RSET
Call by value
A function can be invoked in two manners
(i) call by value
(ii) call by reference
The call by value method copies the value of actual parameters into formal
parameters i.e the function creates its own copy of arguments and uses
them.
Values of variables a
add(a,b); and b are passed to
Call by value
} X,Y
where the values of void add(int x,int y); Now if you change
variable are passed
to functions { the value of X and Y,
those changes are
--------; not seen in a and b
}
/* program to illustrate the concept of call by value
*/
#include<iostream.h>
#include<conio.h>
void add(int,int);
int main()
{
int a,b;
cout<<“enter values of a and b”<<endl;
cin>>a>>b;
add(a,b);
getch();
return 0;
}
void
add(int
x,int y)
{
int c;
cout<<“addition is”<<c;
c=x+y;
Call by reference
In call by reference method in place of calling a value to the function being
called , a reference to the original variable is passed .i.e the same variable value
can be accessed by any of the two names.
In function call
add(a,b); We write reference
variable for formal
} arguments
arguments
⚫ Function
overloading
Inline Functions
⚫ An inline function is a function that expanded
in line when it is invoked.
⚫ That is the compiler replaces the function call with
the corresponding function code .
⚫ Syntax:
inline function-header
{
Function body
}
Example: