Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
What is function????
• Library functions
– These are the in- -built functions of ‘C++ ’library.
– These are already defined in header files.
– e.g. Cout<<; is a function which is used to print at output.
It is defined in ‘iostream.h ’ file .
• User defined functions.
– Programmer can create their own function in C++ to
perform specific task
a) Actual arguments:-the arguments of calling
function are actual arguments.
b) Formal arguments:-arguments of called function
are formal arguments.
c) Argument list:-means variable name enclosed
within the paranthesis.they must be separated by
comma
d) Return value:-it is the outcome of the function.
the result obtained by the function is sent back to
the calling function through the return statement.
Why use function?
• Writing functions avoids rewriting of the same code
again and again in the program.
• Using function large programs can be reduced to
smaller ones. It is easy to debug and find out the
errors in it.
• Using a function it becomes easier to write program
to keep track of what they are doing.
Function Declaration
ret_type func_name(data_type par1,data_type par2);
Function Defination
ret_type func_name(data_type par1,data_type
par2)
{
}
Function Call
func_name(data_type par1,data_type par2);
Function prototype
#include<conio.h>
void main()
{
clrscr();
int a=10,b=20;
void mul(int,int);
mul(a,b); /*actual arguments
getch();
}
void mul(int x, int y) /*formal arguments
{
int s;
s=x*y;
Cout<<“mul is” << s;
}
A function with parameter and return value
#include<conio.h>
void main()
{
clrscr();
int a=10,b=20,c;
int max(int,int);
c=max(a,b);
Cout<<“greatest no is” <<c;
getch();
}
int max(int x, int y)
{
if(x>y)
return(x);
else
{
return(y);
}
}
A function without parameter and return value
#include<conio.h>
void main()
{
clrscr();
int a=10,b=20;
int sum();
int c=sum(); /*actual arguments
Cout<<“sum is”<< c;
getch();
}
int sum() /*formal arguments
{
int x=10,y=30;
return(x+y); /*return value
}
Some important points
• Pass By Value
• Pass By Reference
• Pass By Pointer\address
Call By Value
Void main()
{
int a=10 ,b=25;
void swap(int *,int *);
Cout<<“before function calling”<<a<<b;
swap(&a,&b);
Cout<<“after function calling”<<a<<b;
getch();
}
void swap(int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
Cout<<“value is”<<*x<<*y;
}
Output:
double someMoney;
.
. 3*2*fact(1)
3*fact(2) 3*fact(2)
fact(3) fact(3)
fact(3)
3*2*1
3*2*fact(1)
3*fact(2)
fact(3)
Fibonacci using recursion
int main()
{
int fib(int);
int n;
cout<<“enter the number of terms=“;
cin>>n;
for(int i=1;i<=n;i++)
cout<<fib(i)<<“ “;
getch();
return o;
}
int fib(int m)
{
if(m==1|| m==2)
Advantages of recursion
1. It make program code compact which is easier to
write and understand.
2. It is used with the data structures such as
linklist,stack,queues etc.
3. It is useful if a solution to a problem is in repetitive
form.
4. The compact code in a recursion simplifies the
compilation as less number of lines need to be
compiled.
Disadvantages
What is overloading
– Overloading means assigning multiple
meanings to a function name or operator
symbol
– It allows multiple definitions of a function with
the same name, but different signatures.
C++ supports
– Function overloading
– Operator overloading
Why is Overloading Useful?
void sum(double,double);
void sum(char,char);
void main()
int a=10,b=20 ;
double c=7.52,d=8.14;
Void area(int,int);
Void area(int,int,int);
Int main()
Int side=10,le=5,br=6,a=4,b=5,c=6;
Area(side);
Area(le,br);
Area(a,b,c);
Getch();
Return 0;
Void area(int x)
{ cout<<“area is”<<x*x;
{cout<<“area of rectang;e”=<<x*y;
SCOPE RULES
Scope
{
int a = y;
cout << a << endl;
}
cout << a << endl;
}
MANIPULATOR FUNCTIONS
• These are special stream functions that change
certain characteristics of input and output.
• The main advantage of using manipulator functions
is that they facilitate the formatting of input and
output stream.
• To carry out the operations of these manipulator
functions the header file <iomanip.h> must be
included.
Following are the standard manipulators normally used in
the stream classes:
• endl
• hex,dec,oct
• setbase
• setw
• setfill
• setprecision
endl