SlideShare a Scribd company logo
2
Most read
8
Most read
9
Most read
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;\n Please Insert value of x to find the answer of this equation :  result=x*x+2*x-x/3+23\n x=&quot;; cin>>x; int result= x*x+2*x-x/3+23; cout<<&quot;result = &quot;<<result; cout<<&quot;\n try another x =&quot;; cin>>x; result= x*x+2*x-x/3+23; cout<<&quot;result 2 = &quot;<<result; cout<<&quot;\n 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;\n Please Insert value of x to find the answer of this equation :  result=x*x+2*x-x/3+23\n x=&quot;; cin>>x; cout<<&quot;result = &quot;<<result(x); cout<<&quot;\n try another x =&quot;; cin>>x; cout<<&quot;result 2 = &quot;<<result(x); cout<<&quot;\n 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;\n Please Insert value of x to find the answer of this equation :  result=x*x+2*x-x/3+23\n x=&quot;; cin>>x; cout<<&quot;result = &quot;<<result(x); cout<<&quot;\n try another x =&quot;; cin>>x; cout<<&quot;result 2 = &quot;<<result(x); cout<<&quot;\n 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

PPTX
Strings in C language
PPT
Functions in C++
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
PPTX
Functions in c
PPTX
C Programming: Control Structure
PPT
RECURSION IN C
PPT
Functions in c++
PPT
FUNCTIONS IN c++ PPT
Strings in C language
Functions in C++
CONDITIONAL STATEMENT IN C LANGUAGE
Functions in c
C Programming: Control Structure
RECURSION IN C
Functions in c++
FUNCTIONS IN c++ PPT

What's hot (20)

PPSX
C lecture 4 nested loops and jumping statements slideshare
PPTX
Functions in c++
PPT
16717 functions in C++
 
PPTX
C++ programming function
PPTX
Loops c++
PPTX
Function in c
PPTX
Function in C program
PPTX
PPT
Memory allocation in c
PPT
Input and output in C++
PPTX
Functions in c language
PPTX
Templates in C++
PPTX
Templates in c++
PPTX
Control and conditional statements
PPTX
Conditional and control statement
PPTX
Introduction to c++
PPTX
Branching statements
PPTX
Functions in C
PPTX
Switch case in C++
PPTX
Pointers in c++
C lecture 4 nested loops and jumping statements slideshare
Functions in c++
16717 functions in C++
 
C++ programming function
Loops c++
Function in c
Function in C program
Memory allocation in c
Input and output in C++
Functions in c language
Templates in C++
Templates in c++
Control and conditional statements
Conditional and control statement
Introduction to c++
Branching statements
Functions in C
Switch case in C++
Pointers in c++
Ad

Similar to C++ Function (20)

PDF
how to reuse code
PPTX
C++ Functions.pptx
PPTX
functions of C++
PPTX
Amit user defined functions xi (2)
PPT
Chapter 1.ppt
PDF
PPTX
Cs1123 8 functions
PPT
PPTX
C++ Functions
PPTX
Part 3-functions1-120315220356-phpapp01
PPT
User Defined Functions
PDF
Function notes 2
PDF
Function notes
PPTX
Silde of the cse fundamentals a deep analysis
PDF
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
PPT
Chapter Introduction to Modular Programming.ppt
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
PPTX
Intro To C++ - Class #19: Functions
DOC
Functions
how to reuse code
C++ Functions.pptx
functions of C++
Amit user defined functions xi (2)
Chapter 1.ppt
Cs1123 8 functions
C++ Functions
Part 3-functions1-120315220356-phpapp01
User Defined Functions
Function notes 2
Function notes
Silde of the cse fundamentals a deep analysis
All chapters C++ - Copy.pdfyttttttttttttttttttttttttttttt
Chapter Introduction to Modular Programming.ppt
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
Intro To C++ - Class #19: Functions
Functions
Ad

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
master seminar digital applications in india
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Basic Mud Logging Guide for educational purpose
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
RMMM.pdf make it easy to upload and study
PPTX
Cell Structure & Organelles in detailed.
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
VCE English Exam - Section C Student Revision Booklet
STATICS OF THE RIGID BODIES Hibbelers.pdf
master seminar digital applications in india
Microbial diseases, their pathogenesis and prophylaxis
Basic Mud Logging Guide for educational purpose
Module 4: Burden of Disease Tutorial Slides S2 2025
Anesthesia in Laparoscopic Surgery in India
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Week 4 Term 3 Study Techniques revisited.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
TR - Agricultural Crops Production NC III.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
RMMM.pdf make it easy to upload and study
Cell Structure & Organelles in detailed.
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Microbial disease of the cardiovascular and lymphatic systems
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
human mycosis Human fungal infections are called human mycosis..pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

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;\n Please Insert value of x to find the answer of this equation : result=x*x+2*x-x/3+23\n x=&quot;; cin>>x; int result= x*x+2*x-x/3+23; cout<<&quot;result = &quot;<<result; cout<<&quot;\n try another x =&quot;; cin>>x; result= x*x+2*x-x/3+23; cout<<&quot;result 2 = &quot;<<result; cout<<&quot;\n 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;\n Please Insert value of x to find the answer of this equation : result=x*x+2*x-x/3+23\n x=&quot;; cin>>x; cout<<&quot;result = &quot;<<result(x); cout<<&quot;\n try another x =&quot;; cin>>x; cout<<&quot;result 2 = &quot;<<result(x); cout<<&quot;\n 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;\n Please Insert value of x to find the answer of this equation : result=x*x+2*x-x/3+23\n x=&quot;; cin>>x; cout<<&quot;result = &quot;<<result(x); cout<<&quot;\n try another x =&quot;; cin>>x; cout<<&quot;result 2 = &quot;<<result(x); cout<<&quot;\n 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