Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
C++ FUNCTION  cpplab.pbworks.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
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
Example   #include <iostream.h> int main () { int x; cout<<&quot; Welcome here &quot;; cout<<&quot; Please Insert value of x to find the answer of this equation :  result=x*x+2*x-x/3+23 x=&quot;; cin>>x; int result= x*x+2*x-x/3+23; cout<<&quot;result = &quot;<<result; cout<<&quot; try another x =&quot;; cin>>x; result= x*x+2*x-x/3+23; cout<<&quot;result 2 = &quot;<<result; cout<<&quot; try another x =&quot;; cin>>x; result= x*x+2*x-x/3+23; cout<<&quot;result 3 = &quot;<<result; return 0; } cpplab.pbworks.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
Example  #include <iostream.h> int result (int y); int main () { int x; cout<<&quot; Welcome here &quot;; cout<<&quot; Please Insert value of x to find the answer of this equation :  result=x*x+2*x-x/3+23 x=&quot;; cin>>x; cout<<&quot;result = &quot;<<result(x); cout<<&quot; try another x =&quot;; cin>>x; cout<<&quot;result 2 = &quot;<<result(x); cout<<&quot; try another x =&quot;; cin>>x; cout<<&quot;result 3 = &quot;<<result(x); return 0; } int result(int y) { return ( y*y+2*y-y/3+23); } cpplab.pbworks.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
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
Example   #include <iostream.h> int result (int y); int main () { int x; cout<<&quot; Welcome here &quot;; cout<<&quot; Please Insert value of x to find the answer of this equation :  result=x*x+2*x-x/3+23 x=&quot;; cin>>x; cout<<&quot;result = &quot;<<result(x); cout<<&quot; try another x =&quot;; cin>>x; cout<<&quot;result 2 = &quot;<<result(x); cout<<&quot; try another x =&quot;; cin>>x; cout<<&quot;result 3 = &quot;<<result(x); return 0; } int result(int y) { return ( y*y+2*y-y/3+23); } Function prototype Function  call Function Definition cpplab.pbworks.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
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
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
Example   #include <iostream.h> int   circle  (double  r ); int main () { double reduis; cout<<&quot;enter the reduis to calculate the area of circle :&quot;; cin>>reduis; cout<<&quot;The area of cirle is :&quot;<< circle ( reduis ); return 0; } int  circle  (double  r ) { double area=3.14*r*r; return area; } Function prototype Function prototype Function prototype Function call Function  definition  cpplab.pbworks.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
Return value  #include <iostream.h> double  circle (double r); int main () { double reduis; cout<<&quot;enter the reduis to calculate the area of circle :&quot;; cin>>reduis; cout<<&quot;The area of cirle is :&quot;<<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
Parameter #include <iostream.h> double   circle ( double  r); int main () { double  reduis; cout<<&quot;enter the reduis to calculate the area of circle :&quot;; cin>>reduis; cout<<&quot;The area of cirle is :&quot;<<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.com We use ”return ” because the function return value with  type  double
If you have any question please ask .. You can send email to  [email_address]   You can wrtie a comment if you want any thing .. ^_^ cpplab.pbworks.com

More Related Content

C++ Function

  • 1. C++ FUNCTION cpplab.pbworks.com
  • 2. 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
  • 3. 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
  • 4. Example #include <iostream.h> int main () { int x; cout<<&quot; Welcome here &quot;; cout<<&quot; Please Insert value of x to find the answer of this equation : result=x*x+2*x-x/3+23 x=&quot;; cin>>x; int result= x*x+2*x-x/3+23; cout<<&quot;result = &quot;<<result; cout<<&quot; try another x =&quot;; cin>>x; result= x*x+2*x-x/3+23; cout<<&quot;result 2 = &quot;<<result; cout<<&quot; try another x =&quot;; cin>>x; result= x*x+2*x-x/3+23; cout<<&quot;result 3 = &quot;<<result; return 0; } cpplab.pbworks.com
  • 5. 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
  • 6. Example #include <iostream.h> int result (int y); int main () { int x; cout<<&quot; Welcome here &quot;; cout<<&quot; Please Insert value of x to find the answer of this equation : result=x*x+2*x-x/3+23 x=&quot;; cin>>x; cout<<&quot;result = &quot;<<result(x); cout<<&quot; try another x =&quot;; cin>>x; cout<<&quot;result 2 = &quot;<<result(x); cout<<&quot; try another x =&quot;; cin>>x; cout<<&quot;result 3 = &quot;<<result(x); return 0; } int result(int y) { return ( y*y+2*y-y/3+23); } cpplab.pbworks.com
  • 7. 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
  • 8. 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
  • 9. Example #include <iostream.h> int result (int y); int main () { int x; cout<<&quot; Welcome here &quot;; cout<<&quot; Please Insert value of x to find the answer of this equation : result=x*x+2*x-x/3+23 x=&quot;; cin>>x; cout<<&quot;result = &quot;<<result(x); cout<<&quot; try another x =&quot;; cin>>x; cout<<&quot;result 2 = &quot;<<result(x); cout<<&quot; try another x =&quot;; cin>>x; cout<<&quot;result 3 = &quot;<<result(x); return 0; } int result(int y) { return ( y*y+2*y-y/3+23); } Function prototype Function call Function Definition cpplab.pbworks.com
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. Example #include <iostream.h> int circle (double r ); int main () { double reduis; cout<<&quot;enter the reduis to calculate the area of circle :&quot;; cin>>reduis; cout<<&quot;The area of cirle is :&quot;<< circle ( reduis ); return 0; } int circle (double r ) { double area=3.14*r*r; return area; } Function prototype Function prototype Function prototype Function call Function definition cpplab.pbworks.com
  • 14. 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
  • 15. Return value #include <iostream.h> double circle (double r); int main () { double reduis; cout<<&quot;enter the reduis to calculate the area of circle :&quot;; cin>>reduis; cout<<&quot;The area of cirle is :&quot;<<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
  • 16. Parameter #include <iostream.h> double circle ( double r); int main () { double reduis; cout<<&quot;enter the reduis to calculate the area of circle :&quot;; cin>>reduis; cout<<&quot;The area of cirle is :&quot;<<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.com We use ”return ” because the function return value with type double
  • 17. If you have any question please ask .. You can send email to [email_address] You can wrtie a comment if you want any thing .. ^_^ cpplab.pbworks.com