C++ Function
Transcript of C++ Function
C++ FUNCTION C++ FUNCTION
cpplab.pbworks.com
www.company.com
C++ Function
In this lecture we will learn (what , why ,and how ) using FUNCTION .
So Sharp your pen and perpare your self to start .
cpplab.pbworks.com
www.company.com
Why We need Function ?
Sometime you reach a step where you have to do the same procedure many times.
For example: you have to find the the result of equation each time user enter value of x .
result= x*x+2*x-x/3+23.
Let's see code for that .
cpplab.pbworks.com
www.company.com
Example
#include <iostream.h>
int main ()
{
int x;
cout<<" Welcome here ";
cout<<"\n Please Insert value of x to find the answer of this equation : result=x*x+2*x-x/3+23\n x=";
cin>>x;
int result= x*x+2*x-x/3+23;
cout<<"result = "<<result;
cout<<"\n try another x =";
cin>>x;
result= x*x+2*x-x/3+23;
cout<<"result 2 = "<<result;
cout<<"\n try another x =";
cin>>x;
result= x*x+2*x-x/3+23;
cout<<"result 3 = "<<result;
return 0;
}
cpplab.pbworks.com
www.company.com
We can shortcut this code and put the code of calculating equation into box and call this box each time user enter new number .
cpplab.pbworks.com
www.company.com
Example
#include <iostream.h>
int result (int y);
int main ()
{
int x;
cout<<" Welcome here ";
cout<<"\n Please Insert value of x to find the answer of this equation : result=x*x+2*x-x/3+23\n x=";
cin>>x;
cout<<"result = "<<result(x);
cout<<"\n try another x =";
cin>>x;
cout<<"result 2 = "<<result(x);
cout<<"\n try another x =";
cin>>x;
cout<<"result 3 = "<<result(x);
return 0;
}
int result(int y)
{
return ( y*y+2*y-y/3+23);
}
cpplab.pbworks.com
www.company.com
Here We Go
What we did is to move equation calculation into ”function” .
This function do this calculation each time we call its name ”result” in main function .
cpplab.pbworks.com
www.company.com
What is a Function ?
Function is subprogram or procedure that has a purpose to perform some tasks .
Function has 3 main parts : Function Prototype → declare the function .
Function Definition → define what the function will do .
Function Call → ask the function to come and perform the task.
cpplab.pbworks.com
www.company.com
Example
#include <iostream.h>
int result (int y);
int main ()
{
int x;
cout<<" Welcome here ";
cout<<"\n Please Insert value of x to find the answer of this equation : result=x*x+2*x-x/3+23\n x=";
cin>>x;
cout<<"result = "<<result(x);
cout<<"\n try another x =";
cin>>x;
cout<<"result 2 = "<<result(x);
cout<<"\n try another x =";
cin>>x;
cout<<"result 3 = "<<result(x);
return 0;
}
int result(int y)
{
return ( y*y+2*y-y/3+23);
}
Function prototype Function call
FunctionDefinition
cpplab.pbworks.com
www.company.com
Function Prototype
Funtion Prototype is used to declare the function .
Function prototype statement is optional ..
You have to write it before main () and before any funtion you call in it .
The main format is :
type name (type parameter1, type parameter2, ...);
Where : Type : datatype of data returned by function . Name : unique name of function . Group of parameters : values you take from calling
place
cpplab.pbworks.com
www.company.com
Function Definition
Function Definition : group of statments that perform when the funtion is called .
You can define function before main () or after .
You have to define it before main () if you don't use function prototype .
The main format :
type name (type parameter1, type parameter2, …)
{ statements } Function definition should be same as
function prototype.
cpplab.pbworks.com
www.company.com
Function Call
Function means nothing without calling .
Calling function tell the complier to find function defintion and execute the its statements .
Function call can be used inside main () .
The main format :
Name (parameter1, parameter2 ..)
cpplab.pbworks.com
www.company.com
Example
#include <iostream.h>
int circle (double r);
int main ()
{
double reduis;
cout<<"enter the reduis to calculate the area of circle :";
cin>>reduis;
cout<<"The area of cirle is :"<<circle(reduis);
return 0;
}
int circle (double r)
{
double area=3.14*r*r;
return area;
}
Function prototypeFunction prototypeFunction prototype
Function call
Function definition
cpplab.pbworks.com
www.company.com
Remmber ..
You can return none or 1 value .
You can send none or many parameters.
When function return value , you have to use ”return” ate the end of function defintion .
When funtion doesn't return value , use ” void” as data type of return value and dont use ”return ” at the end of function definition .
If you don't send any parameter , leave the praces empty .
cpplab.pbworks.com
www.company.com
Return value
#include <iostream.h>
double circle (double r);
int main ()
{
double reduis;
cout<<"enter the reduis to calculate the area of circle :";
cin>>reduis;
cout<<"The area of cirle is :"<<circle(reduis);
return 0;
}
double circle (double r)
{
double area=3.14*r*r;
return area;
}
double here means function will return one value with type double
Because the function return double value , so we Have to call this function inside cout staement or
assign it to any variable with same type e.g. double m=circle (reduis);
cpplab.pbworks.com
We use ”return ” because the function return value with type double
www.company.com
Parameter
#include <iostream.h>
double circle (double r);
int main ()
{
double reduis;
cout<<"enter the reduis to calculate the area of circle :";
cin>>reduis;
cout<<"The area of cirle is :"<<circle(reduis);
return 0;
}
double circle (double r)
{
double area=3.14*r*r;
return area;
}
Double r here means function will pass one parameter form place of calling with type double
Because the function return double value , so we Have to call this function inside cout staement or
assign it to any variable with same type e.g. double m=circle (reduis);
cpplab.pbworks.comWe use ”return ” because the function return value with type double
www.company.com
If you have any question please ask ..
You can send email to
You can wrtie a comment if you want any thing ..
^_^
cpplab.pbworks.com