A function is a sub program which performs a specific task.
Or
A block of code is written outside the main.
Advantages of using functions:
Size of the program reduces
It occupies less memory
Repetition of the data will not be there because once a function is defined it can be called any number of times.
Separation of a code will be there
Easy to understand
Easy to fine errors
Execution of the program the program will be fast.
Types of functions:
C++ is supported to following types of Functions
Predefined Functions
User defined functions
Predefined Functions:
The functions which are already defined c language.
Ex: sqrt(),pow().cos()…..etc.
User defined functions:
The functions which are defined by the user are called as user defined functions.
The contents of this function can be change when ever the user wants.
Syntax:
Returntype functionname (argument_list)
{
…
…
}
Return type:
It represents the type of the value function is going to return.
If return type is int function will return integer value.
If return type is float function will return float value.
If return type is void it represents that function will not return any value.
Return statement is used for returning a value to the caller.
Syntax:
Return value;
Note: at a time a function can return only one value.
Function Name:
It can be any valid name.
Argument list:
It represents the type of the arguments and the no. of arguments the function takes.
Body of the Function:
The content of the function is called as body of the function.
Types of functions based on arguments list and return type.
C++- Supports 4 types functions based on argument list and return type.
- Function with no arguments and no return value.
- Function with arguments and no return value.
- Function with arguments and with return value.
- Function with no arguments and with return value.
Function with no arguments
and no return value
|
Function with arguments and
no return value
|
Function with arguments and
with return value
|
Function with no arguments
and with return value
|
Syntax:
void fn ( )
{
…
…
}
|
Syntax:
void fn (al)
{
…
…
}
|
Syntax:
rt fn (al)
{
…
…
}
|
Syntax:
rt fn ( )
{
…
…
}
|
rt: return type , fn :function name ,
al: arguments list
|
|||
// write a program to find
sum of two numbers
|
|||
#include<iostream.h>
#include<conio.h>
void sum ();
void main ();
{
clrscr();
sum();
getch();
}
void sum()
{
int
a,b,c;
cout<<“\nEnter
the values of a,b:”;
cin>>
a>>b;
c=a+b;
cout<<“\nsum=”<<c;
}
Result:
Input:
Enter
the values of a,b: 2 3
Output:
Sum=5
|
#include<iostream.h>
#include<conio.h>
void sum (int a, int b);
void main ();
{
int
x,y;
clrscr();
cout<<“\nEnter
the values of x,y:”;
cin>>
x>>y;
sum(x,y);
getch();
}
void sum(int a, int b)
{
int
c;
c=a+b;
cout<<“\nsum=”<<c;
}
Result:
Input:
Enter
the values of a,b: 2 3
Output:
Sum=5
|
#include<iostream.h>
#include<conio.h>
int sum (int a, int b);
void main ();
{
int
x,y;
clrscr();
cout<<“\nEnter
the values of x,y:”;
cin>>
x>>y;
rv=sum(x,y);
cout<<“\nsum=”<<rv;
getch();
}
int sum(int a, int b)
{
int
c;
c=a+b;
return
c;
}
Result:
Input:
Enter
the values of a,b: 2 3
Output:
Sum=5
|
#include<iostream.h>
#include<conio.h>
int sum ();
void main ();
{
int
rv;
clrscr();
rv=sum();
cout<<“\nsum=”<<rv;
getch();
}
int sum()
{
int
a,b,c;
cout<<“\nEnter
the values of a,b:”;
cin>>
a>>b;
c=a+b;
return
c;
}
Result:
Input:
Enter
the values of a,b: 2 3
Output:
Sum=5
|
Note:
After
the execution of function completes control returns back to the caller.
|
Note:
//x,
y are actual parameters.
//a, b are formal parameters.
Actual Parameters:
The
parameters passed by the user by calling the function are called as actual
parameters.
Formal Parameters:
The
parameters used in the function definition are called formal parameters
Note:
While
calling the function we have to pass the exact no of arguments the function
takes.
If we
pass less no of arguments then the function takes, it generates an error
message two few parameters to call the function.
If we
pass more no of arguments then the function takes, it generates an error
message extra parameters in calling the function.
|
Note:
Return
statement is used for returning a value.
Syntax:
return
value;
After
execution of this statement control returns back to the caller.
At a
time a function can return only one value.
|
Note: the variables declared in one function are not
visible in other functions so we must declare the variables in which they are
used.
//write a program to find maximum of two numbers
|
|||
#include<iostream.h>
#include<conio.h>
void max ();
void main ();
{
clrscr();
max();
getch();
}
void max()
{
int
a,b;
cout<<“\nEnter
the values of a,b:”;
cin>>
a>>b;
if(a>b)
{
cout<<“\nmax=”<<a;
}
else
{
cout<<“\nmax=”<<b;
}
}
Result:
Input:
Enter
the values of a,b: 23 36
Output:
Max=36
|
#include<iostream.h>
#include<conio.h>
void max (int a, int b);
void main ();
{
int
x,y;
clrscr();
cout<<“\nEnter
the values of x,y:”;
cin>>
x>>y;
max(x,y);
getch();
}
void max(int a, int b)
{
if(a>b)
{
cout<<“\nmax=”<<a;
}
else
{
cout<<“\nmax=”<<b;
}
}
Result:
Input:
Enter
the values of a,b: 23 36
Output:
Max=36
|
#include<iostream.h>
#include<conio.h>
int max (int a, int b);
void main ();
{
int
x,y;
clrscr();
cout<<“\nEnter
the values of x,y:”;
cin>>
x>>y;
rv=max(x,y);
cout<<“\nmax=”<<rv;
getch();
}
int max(int a, int b)
{
if(a>b)
return
a;
else
return
b;
}
Result:
Input:
Enter
the values of a,b: 23 36
Output:
Max=36
|
#include<iostream.h>
#include<conio.h>
int max ();
void main ();
{
int
rv;
clrscr();
rv=max();
cout<<“\nmax=”<<rv;
getch();
}
int max()
{
int
a,b;
cout<<“\nEnter
the values of a,b:”;
cin>>
a>>b;
if(a>b)
return
a;
else
return
b;
}
Result:
Input:
Enter
the values of a,b: 23 36
Output:
Max=36
|
Recursive function:
The function called it self is known as Recursive
function.
// program
|
#include<iostream.h>
#include<conio.h>
long int fact (long int n);
void main ();
{
int n;
clrscr();
cout<<“\nEnter
the value of n:”;
cin>>
n;
rv=fact(n);
cout<<“\nfact
of “<<n<<”=”<<rv;
getch();
}
long int fact(long int n)
{
if(n==0)
return
1;
else
return
n*fact(n-1);
}
Result:
Input:
Enter
the value of n: 5
Output:
Fact of
5=120
|
Function with default
arguments:
In C while calling the function we have to pass exact
elements the function takes where as in C++ it is optional to pass all the
arguments if a function is initialized from with default value.
Default value must be initialized from right to left,
without initializing the right one we can’t initialize left.
Correct
initialization
|
wrong
initialization
|
sum(int
a,int b,int c=1);
sum(int
a,int b=1,int c=1);
sum(int
a=1,int b=1,int c=1);
|
sum(int
a=1,int b,int c);
sum(int
a,int b=1,int c);
sum(int
a=1,int b,int c=1);
sum(int
a=1,int b=1,int c);
|
// program
|
#include<iostream.h>
#include<conio.h>
void sum(int a=1,int b=1,int
c=1);
void main()
{
clrscr();
sum(10,20,30);
sum(2,3);
sum(12);
sum();
getch();
}
void sum(int a,int b,int c)
{
cout<<"\n\na="<<a;
cout<<"\nb="<<b;
cout<<"\nc="<<c;
cout<<"\nsum="<<(a+b+c);
}
|
Result:
Output:
a=10
b=20
c=30
sum=60
a=2
b=3
c=1
sum=6
a=12
b=1
c=1
sum=14
a=1
b=1
c=1
sum=3
|
Function Overloading:
In C, within one name we have only one function where as
in C++ with one name we have any no. of functions varying different types of
arguments is called as function overloading.
In this function identified, if the function name is same
then the compiler check the no. of arguments.
ex:
sum(int,int,int)
sum(int,int);
If the no. of arguments is also same then it will be
check the type of arguments.
ex:
sum(int,int);
sum(float,float);
If the type of arguments is also same then it will be
check the order of arguments.
ex:
sum(int,float);
sum(float,int)
If the order of arguments is also same then the compiler
displays the error message.
// program
|
// program
|
#include<iostream.h>
#include<conio.h>
void sum(int a,int b,int c);
void sum(int a,int b);
void main()
{
clrscr();
sum(20,90,63);
sum(52,23);
getch();
}
void sum(int a,int b,int c)
{
cout<<"\n\na="<<a;
cout<<"\nb="<<b;
cout<<"\nc="<<c;
cout<<"\nsum="<<(a+b+c);
}
void sum(int a,int b)
{
cout<<"\n\na="<<a;
cout<<"\nb="<<b;
cout<<"\nsum="<<(a+b);
}
|
#include<iostream.h>
#include<conio.h>
void findarea(float s);
void findarea(float l,float
b);
void main()
{
clrscr();
findarea(6.3);
findarea(5.2,2.3);
getch();
}
void findarea(float s)
{
cout<<"\n\nside
of the square="<<s;
cout<<"\narea
of the square="<<(s*s);
}
void findarea(float l,float b)
{
cout<<"\n\nlength
of the rectangle="<<l;
cout<<"\n
breadth of the rectangle="<<b;
cout<<"\narea
of the rectangle="<<(l*b);
}
|
Result:
Output:
a=20
b=90
c=63
sum=173
a=52
b=23
sum=75
|
Result:
Output:
side
of the square=6.3
area
of the square=39.690002
length
of the rectangle=5.2
breadth
of the rectangle=2.3
area
of the rectangle=11.959999
|
Inline Function:
The function is preceding with key word inline then it is
called inline function.
The inline functions are similar to macros which replace
code at function call.
The difference is macros can’t handle exception(runtime
errors) where as inline functions can handle runtime exceptions.
When the function is small i.e. maximum one or two lines
then it can be declared as inline function.
// program
|
#include<iostream.h>
#include<conio.h>
Inline int sum(int a,int
b,int c)
{
return
a+b;
}
void main()
{
int
a,b,rv;
clrscr();
cout<<”enter
the values of a,b:”;
cin>>a>>b;
rv=sum(a,b);
cout<<”\nsum=”<<rv;
getch();
}
|
Result:
Input:
enter
the values of a,b:5
6
Output:
sum=11
|
No comments:
Post a Comment