Professional Documents
Culture Documents
What is function????
Library functions
These are the in- -built functions of C++ library.
These are already defined in header files.
e.g. getch() is a function which is used to get a
char from keyboard. It is defined in conio.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
Inline function
#include<iostream>
using namespace std;
inline int max(int x,int y)
{
return (x>y?x:y);
}
int main()
{
int m=10,n=25;
int a,b;
a=max(6,8);
cout<<greatest is<<a;
b=max(m,n);
cout<<greates of m=<<m<< and n=<<n
<<is <<b;
return 0;
}
Output :
Greatest is 8
Greatest of m=10 and n=25 is 25
An inline function is a small function with
no calling overhead
Overhead is avoided because program
control never transfers to the function
A copy of the function statements is placed
directly into the compiled calling program
The inline function appears prior to the
main(), which calls it
Any inline function must precede any
function that calls it, which eliminates the
need for prototyping in the calling function
Disadvantages:
#include<iostream>
using namespace std;
void mul(int,int);
int main()
{
int a=10,b=20;
mul(a,b); /*actual arguments
return 0;
}
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<iostream>
using namespace std;
int max(int,int);
int main()
{
int a=10,b=20,c;
c=max(a,b);
Cout<<greatest no is <<c;
return 0;
}
int max(int x, int y)
{
if(x>y)
return(x);
else
{
return(y);
}
}
A function without parameter and
return value
#include<iostream>
using namespace std;
int main()
{
int a=10,b=20;
int sum();
int c=sum(); /*actual arguments
cout<<sum is<< c;
return 0;
}
int sum() /*formal arguments
{
int x=10,y=30;
return(x+y); /*return value
}
Some important points
endl
hex,dec,oct
setbase
setw
setfill
setprecision
endl
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(int x, int y)
{
cout<<\n sum of integers are<<x+y;
}
ide
uts
{ t o
is
t ex
int a = y; esn
do
cout << a << endl; a
r k !
ro o c
Er e bl
} th
}; };
ARGUMENT PASSING
TECHNIQUES
Argument passing techniques
Pass By Value
Pass By Pointer\address
Pass By reference
Call By Value
double Money;
fact=factorial(x);
else
cout<<\nfactorial of<<x<<=<<fact;
b=a*factorial(a-1);
return 0;
return (b);
} }
Output:
Enter a number=3
Factorial0f 3=6
Stack representation
.
. 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 m)
int fib(int);
{
int n;
if(m==1|| m==2)
cout<<enter the number of terms=;
return(1);
cin>>n;
else
for(int i=1;i<=n;i++)
return(fib(m-1)+fib(m-2));
cout<<fib(i)<< ;
}
return 0;
}
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