Lecture10 24august CAP202
Lecture10 24august CAP202
streams
Outline
• Creating User Defined Functions
• Functions With Default Arguments
• Inline Functions
What is function????
• Function is a self contained block of
statements that perform a coherent task of
some kind.
• Every C++ program can be a thought of the
collection of functions.
• main( ) is also a function.
Types of Functions.
• Library functions
– These are the in- -built functions of ‘C++ ’library.
– These are already defined in header files.
• User defined functions.
– Programmer can create their own function in C++
to perform specific task
Why use function?
//Function Defination
rent_type func_name(data_type par1,data_type par2)
{
// body of the function
}
//Function Call
func_name(par1,par2);
Function prototype
• A prototype statement helps the compiler to
check the return type and arguments type of
the function.
• A prototype function consist of the functions
return type, name and argument list.
• Example
– int sum( int x, int y);
Example
#include<iostream>
int main()
{
int a, b;
cout << "Enter the two numbers: ";
cin >> a >> b;
sum(a, b); // function calling
cin.get(); // or use cin.get() to pause the program
return 0;
}
#include<iostream.h>
void mul(int,int);
void main()
{
clrscr();
int a=10,b=20;
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<iostream.h>
int max(int,int);
void main()
{
clrscr();
int a=10,b=20,c;
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<iostream.h>
int sum();
void main()
{
clrscr();
int a=10,b=20;