SlideShare a Scribd company logo
1 of 16
Types of function
Introduction to
Functions
Introduction to Functions
• A complex problem is often easier to solve by
dividing it into several smaller parts, each of
which can be solved by itself.
• This is called structured programming.
• These parts are sometimes made into
functions in C++.
• main() then uses these functions to solve
the original problem.
Advantages of Functions
• Functions separate the concept (what is done)
from the implementation (how it is done).
• Functions make programs easier to
understand.
• Functions can be called several times in the
same program, allowing the code to be
reused.
C++ Functions
• C++ allows the use of both internal (user-
defined) and external functions.
• External functions (e.g., abs, ceil, rand,
sqrt, etc.) are usually grouped into specialized
libraries (e.g., iostream, stdlib, math,
etc.)
User-Defined Functions
• C++ programs usually have the following form:
// include statements
// function prototypes
// main() function
// function definitions
Function Input and Output
Function Definition
A function definition has the following syntax:
<type> <function name>(<parameter list>){
<local declarations>
<sequence of statements>
}
For example: Definition of a function that computes the absolute value
of an integer:
int absolute(int x){
if (x >= 0) return x;
else return -x;
}
Function Call
• A function call has the following syntax:
<function name>(<argument list>)
Example: int distance = absolute(-5);
– The result of a function call is a value of type
<type>
Arguments/Parameters
• one-to-one correspondence between the
arguments in a function call and the
parameters in the function definition.
int argument1;
double argument2;
// function call (in another function, such as main)
result = thefunctionname(argument1, argument2);
// function definition
int thefunctionname(int parameter1, double parameter2){
// Now the function can use the two parameters
// parameter1 = argument 1, parameter2 = argument2
Absolute Value
#include <iostream>
using namespace std;
int absolute (int);// function prototype for absolute()
int main(){
int num, answer;
cout << "Enter an integer (0 to stop): ";
cin >> num;
while (num!=0){
answer = absolute(num);
cout << "The absolute value of " << num
<< " is: " << answer << endl;
cin >> num; }
return 0; }
// Define a function to take absolute value of an integer
int absolute(int x){
if (x >= 0) return x;
else return -x; }
Function Prototype
• The function prototype declares the input and
output parameters of the function.
• The function prototype has the following syntax:
<type> <function name>(<type list>);
• Example: A function that returns the absolute
value of an integer is: int
absolute(int);
Function Definition
• The function definition can be placed
anywhere in the program after the function
prototypes.
• If a function definition is placed in front of
main(), there is no need to include its
function prototype.
Absolute Value (alternative)
• Note that it is possible to omit the function prototype if the function is placed before it
is called.
#include <iostream>
using namespace std;
int absolute(int x){
if (x >= 0) return x;
else return -x; }
int main(){
int num, answer;
cout << "Enter an integer (0 to stop): ";
cin >> num;
while (num!=0){
answer = absolute(num);
cout << "The absolute value of " << num
<< " is: " << answer << endl;
cin >> num; }
return 0; }
Function of three parameters
#include <iostream>
using namespace std;
double total_second(int, double ,double );
int main(){
cout << total_second(1,1.5, 2) << endl;
return 0;
}
double total_second( int hour, double minutes,
double second)
{
return hour*3600 + minutes * 60 + second;
}
Printing the Diamond Pattern as a Function
void diamond(int size)
{
int row, space, star;
for(row=1; row<=size; row++){ //top half
for(space=1; space<=size-row; space++)
cout << " ";
for(star=1; star<=2*row-1; star++)
cout << "*";
cout << endl ;
}
for(row=size -1; row>=1; row--){ //bottom half
for(space=1; space<=size-row; space++)
cout << " ";
for(star=1; star<=2*row-1; star++)
cout << "*";
cout << endl ;
} }
Calculating the Area of a Circle
with a Function

More Related Content

What's hot (20)

Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
Functions in c
Functions in cFunctions in c
Functions in c
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Inline function
Inline functionInline function
Inline function
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Call by value or call by reference in C++
Call by value or call by reference in C++Call by value or call by reference in C++
Call by value or call by reference in C++
 
Tokens expressionsin C++
Tokens expressionsin C++Tokens expressionsin C++
Tokens expressionsin C++
 
Templates
TemplatesTemplates
Templates
 
Hot C++: Rvalue References And Move Semantics
Hot C++: Rvalue References And Move SemanticsHot C++: Rvalue References And Move Semantics
Hot C++: Rvalue References And Move Semantics
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Constructor and destructor
Constructor  and  destructor Constructor  and  destructor
Constructor and destructor
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Function in c
Function in cFunction in c
Function in c
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 

Similar to Function (20)

Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function
FunctionFunction
Function
 
Function in cpu 2
Function in cpu 2Function in cpu 2
Function in cpu 2
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Functions
FunctionsFunctions
Functions
 
Functions
FunctionsFunctions
Functions
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Python_Functions_Unit1.pptx
Python_Functions_Unit1.pptxPython_Functions_Unit1.pptx
Python_Functions_Unit1.pptx
 
Funtions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the topsFuntions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the tops
 
Function
FunctionFunction
Function
 
Function C++
Function C++ Function C++
Function C++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
 
Functions
FunctionsFunctions
Functions
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
Functions_new.pptx
Functions_new.pptxFunctions_new.pptx
Functions_new.pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
 

More from yash patel

English question with answer 301 to 400
English question with answer 301 to 400English question with answer 301 to 400
English question with answer 301 to 400yash patel
 
English grammar pdf for tet
English grammar pdf for tetEnglish grammar pdf for tet
English grammar pdf for tetyash patel
 
solar energy/de report
solar energy/de reportsolar energy/de report
solar energy/de reportyash patel
 
AUTO MAETIC AIR FELLING
AUTO MAETIC AIR FELLINGAUTO MAETIC AIR FELLING
AUTO MAETIC AIR FELLINGyash patel
 
Design report
Design report Design report
Design report yash patel
 
Compasssurveyin
CompasssurveyinCompasssurveyin
Compasssurveyinyash patel
 
The old man and the sea
The old man and the seaThe old man and the sea
The old man and the seayash patel
 
The entity relationship model
The entity relationship modelThe entity relationship model
The entity relationship modelyash patel
 
Laplace transforms
Laplace transforms Laplace transforms
Laplace transforms yash patel
 
Superposition theorem
Superposition theoremSuperposition theorem
Superposition theoremyash patel
 
Permanent magnet moving iron type instruments
Permanent magnet moving iron type instrumentsPermanent magnet moving iron type instruments
Permanent magnet moving iron type instrumentsyash patel
 
Constant and variacles in c
Constant   and variacles in cConstant   and variacles in c
Constant and variacles in cyash patel
 
Database management system
Database management systemDatabase management system
Database management systemyash patel
 
Freedomisnotfree
Freedomisnotfree   Freedomisnotfree
Freedomisnotfree yash patel
 

More from yash patel (20)

English question with answer 301 to 400
English question with answer 301 to 400English question with answer 301 to 400
English question with answer 301 to 400
 
English grammar pdf for tet
English grammar pdf for tetEnglish grammar pdf for tet
English grammar pdf for tet
 
solar energy/de report
solar energy/de reportsolar energy/de report
solar energy/de report
 
euler
 euler euler
euler
 
AUTO MAETIC AIR FELLING
AUTO MAETIC AIR FELLINGAUTO MAETIC AIR FELLING
AUTO MAETIC AIR FELLING
 
Design report
Design report Design report
Design report
 
Compasssurveyin
CompasssurveyinCompasssurveyin
Compasssurveyin
 
Tangent plane
Tangent planeTangent plane
Tangent plane
 
Wings of fire
Wings of fireWings of fire
Wings of fire
 
Pumps
PumpsPumps
Pumps
 
The old man and the sea
The old man and the seaThe old man and the sea
The old man and the sea
 
The entity relationship model
The entity relationship modelThe entity relationship model
The entity relationship model
 
Block diagram
Block diagramBlock diagram
Block diagram
 
Laplace transforms
Laplace transforms Laplace transforms
Laplace transforms
 
Superposition theorem
Superposition theoremSuperposition theorem
Superposition theorem
 
Permanent magnet moving iron type instruments
Permanent magnet moving iron type instrumentsPermanent magnet moving iron type instruments
Permanent magnet moving iron type instruments
 
Constant and variacles in c
Constant   and variacles in cConstant   and variacles in c
Constant and variacles in c
 
Entropy
EntropyEntropy
Entropy
 
Database management system
Database management systemDatabase management system
Database management system
 
Freedomisnotfree
Freedomisnotfree   Freedomisnotfree
Freedomisnotfree
 

Recently uploaded

NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...Amil baba
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...ronahami
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesChandrakantDivate1
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...ppkakm
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxNANDHAKUMARA10
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiessarkmank1
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...drmkjayanthikannan
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Ramkumar k
 

Recently uploaded (20)

NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
Computer Graphics Introduction To Curves
Computer Graphics Introduction To CurvesComputer Graphics Introduction To Curves
Computer Graphics Introduction To Curves
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...Basic Electronics for diploma students as per technical education Kerala Syll...
Basic Electronics for diploma students as per technical education Kerala Syll...
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 

Function

  • 2. Introduction to Functions • A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself. • This is called structured programming. • These parts are sometimes made into functions in C++. • main() then uses these functions to solve the original problem.
  • 3. Advantages of Functions • Functions separate the concept (what is done) from the implementation (how it is done). • Functions make programs easier to understand. • Functions can be called several times in the same program, allowing the code to be reused.
  • 4. C++ Functions • C++ allows the use of both internal (user- defined) and external functions. • External functions (e.g., abs, ceil, rand, sqrt, etc.) are usually grouped into specialized libraries (e.g., iostream, stdlib, math, etc.)
  • 5. User-Defined Functions • C++ programs usually have the following form: // include statements // function prototypes // main() function // function definitions
  • 7. Function Definition A function definition has the following syntax: <type> <function name>(<parameter list>){ <local declarations> <sequence of statements> } For example: Definition of a function that computes the absolute value of an integer: int absolute(int x){ if (x >= 0) return x; else return -x; }
  • 8. Function Call • A function call has the following syntax: <function name>(<argument list>) Example: int distance = absolute(-5); – The result of a function call is a value of type <type>
  • 9. Arguments/Parameters • one-to-one correspondence between the arguments in a function call and the parameters in the function definition. int argument1; double argument2; // function call (in another function, such as main) result = thefunctionname(argument1, argument2); // function definition int thefunctionname(int parameter1, double parameter2){ // Now the function can use the two parameters // parameter1 = argument 1, parameter2 = argument2
  • 10. Absolute Value #include <iostream> using namespace std; int absolute (int);// function prototype for absolute() int main(){ int num, answer; cout << "Enter an integer (0 to stop): "; cin >> num; while (num!=0){ answer = absolute(num); cout << "The absolute value of " << num << " is: " << answer << endl; cin >> num; } return 0; } // Define a function to take absolute value of an integer int absolute(int x){ if (x >= 0) return x; else return -x; }
  • 11. Function Prototype • The function prototype declares the input and output parameters of the function. • The function prototype has the following syntax: <type> <function name>(<type list>); • Example: A function that returns the absolute value of an integer is: int absolute(int);
  • 12. Function Definition • The function definition can be placed anywhere in the program after the function prototypes. • If a function definition is placed in front of main(), there is no need to include its function prototype.
  • 13. Absolute Value (alternative) • Note that it is possible to omit the function prototype if the function is placed before it is called. #include <iostream> using namespace std; int absolute(int x){ if (x >= 0) return x; else return -x; } int main(){ int num, answer; cout << "Enter an integer (0 to stop): "; cin >> num; while (num!=0){ answer = absolute(num); cout << "The absolute value of " << num << " is: " << answer << endl; cin >> num; } return 0; }
  • 14. Function of three parameters #include <iostream> using namespace std; double total_second(int, double ,double ); int main(){ cout << total_second(1,1.5, 2) << endl; return 0; } double total_second( int hour, double minutes, double second) { return hour*3600 + minutes * 60 + second; }
  • 15. Printing the Diamond Pattern as a Function void diamond(int size) { int row, space, star; for(row=1; row<=size; row++){ //top half for(space=1; space<=size-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; } for(row=size -1; row>=1; row--){ //bottom half for(space=1; space<=size-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; } }
  • 16. Calculating the Area of a Circle with a Function