SlideShare a Scribd company logo
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

PPT
Functions in C++
PPT
Function in C Language
PPTX
Recursive Function
PPT
PPT
Functions in C++
PPTX
Pointer arithmetic in c
PPTX
Header files of c++ unit 3 -topic 3
Functions in C++
Function in C Language
Recursive Function
Functions in C++
Pointer arithmetic in c
Header files of c++ unit 3 -topic 3

What's hot (20)

PPTX
Functions in C
PPTX
Function C programming
PPTX
Passing an Array to a Function (ICT Programming)
PPTX
Dynamic memory allocation in c++
PDF
03 function overloading
PPTX
C++ programming function
PPT
Stack a Data Structure
PDF
Pointers
PPTX
PPTX
Types of function call
PPT
Memory allocation in c
PPSX
Function in c
PPTX
Chapter 05 classes and objects
PDF
Functions and modules in python
KEY
Clean code and Code Smells
PDF
PPTX
Tuple in python
PPTX
PPTX
Strings in c++
PPTX
Functions in C
Functions in C
Function C programming
Passing an Array to a Function (ICT Programming)
Dynamic memory allocation in c++
03 function overloading
C++ programming function
Stack a Data Structure
Pointers
Types of function call
Memory allocation in c
Function in c
Chapter 05 classes and objects
Functions and modules in python
Clean code and Code Smells
Tuple in python
Strings in c++
Functions in C
Ad

Similar to Function (20)

PPT
Functions in C++
PPT
PPT
Function in cpu 2
PPTX
Chapter 4
PPTX
Chp8_C++_Functions_Part2_User-defined functions.pptx
PPTX
PPTX
Functions
PPTX
Inline Functions and Default arguments
PPTX
Python_Functions_Unit1.pptx
PPTX
Funtions of c programming. the functions of c helps to clarify all the tops
PPT
PPTX
Function C++
PPT
PPTX
Chapter 1 (2) array and structure r.pptx
PPT
16717 functions in C++
 
PPTX
PDF
PPTX
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
PPTX
C++ Functions
PPT
C++ Functions.ppt
Functions in C++
Function in cpu 2
Chapter 4
Chp8_C++_Functions_Part2_User-defined functions.pptx
Functions
Inline Functions and Default arguments
Python_Functions_Unit1.pptx
Funtions of c programming. the functions of c helps to clarify all the tops
Function C++
Chapter 1 (2) array and structure r.pptx
16717 functions in C++
 
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
C++ Functions
C++ Functions.ppt
Ad

More from yash patel (20)

DOCX
English question with answer 301 to 400
DOCX
English grammar pdf for tet
DOCX
solar energy/de report
PPTX
euler
DOCX
AUTO MAETIC AIR FELLING
DOCX
Design report
PPTX
Compasssurveyin
PPTX
Tangent plane
PPTX
Wings of fire
PPTX
Pumps
PPTX
The old man and the sea
PPT
The entity relationship model
PPTX
Block diagram
PPTX
Laplace transforms
PPTX
Superposition theorem
PPTX
Permanent magnet moving iron type instruments
PPTX
Constant and variacles in c
PPTX
Entropy
PPTX
Database management system
PPTX
Freedomisnotfree
English question with answer 301 to 400
English grammar pdf for tet
solar energy/de report
euler
AUTO MAETIC AIR FELLING
Design report
Compasssurveyin
Tangent plane
Wings of fire
Pumps
The old man and the sea
The entity relationship model
Block diagram
Laplace transforms
Superposition theorem
Permanent magnet moving iron type instruments
Constant and variacles in c
Entropy
Database management system
Freedomisnotfree

Recently uploaded (20)

PDF
PPT on Performance Review to get promotions
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Construction Project Organization Group 2.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Welding lecture in detail for understanding
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
Project quality management in manufacturing
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
additive manufacturing of ss316l using mig welding
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPT on Performance Review to get promotions
Model Code of Practice - Construction Work - 21102022 .pdf
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Construction Project Organization Group 2.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Lecture Notes Electrical Wiring System Components
Welding lecture in detail for understanding
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Project quality management in manufacturing
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Lesson 3_Tessellation.pptx finite Mathematics
additive manufacturing of ss316l using mig welding
Foundation to blockchain - A guide to Blockchain Tech
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026

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