SlideShare a Scribd company logo
1 of 13
Download to read offline
GANDHINAGAR INSTITUTE OF
TECHNOLOGY
Department of Computer Engineering
Function in C++
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>
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.
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;
}
Calculating the Area of a Circle
with a Function
#include<iostream.h>
#include<conio.h>
const float pi=3.14;
float area(float r) //  This is the function for calculating area
{
float ar;
ar=pi*r*r;
return ar;
}
void main()
{
float r,result;
clrscr();
cout<<“nEnter the Radius of Circle: n”;
cin>>r;
result=area(r); //  Function call
cout<<“nArea of Circle: “<<result<<endl;
getch();
}
Functions in C++

More Related Content

What's hot (20)

C++ Function
C++ FunctionC++ Function
C++ Function
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
functions of C++
functions of C++functions of C++
functions of C++
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
INLINE FUNCTION IN C++
INLINE FUNCTION IN C++INLINE FUNCTION IN C++
INLINE FUNCTION IN C++
 
Inline function
Inline functionInline function
Inline function
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
OOP in C++
OOP in C++OOP in C++
OOP in C++
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Functions in c
Functions in cFunctions in c
Functions in c
 

Viewers also liked

Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-classDeepak Singh
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasShahzad Younas
 
Safety rules and earthing
Safety rules and earthingSafety rules and earthing
Safety rules and earthingNikhil Pandit
 
Simplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationSimplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationNikhil Pandit
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default argumentsNikhil Pandit
 
Software Coding- Software Coding
Software Coding- Software CodingSoftware Coding- Software Coding
Software Coding- Software CodingNikhil Pandit
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismJawad Khan
 
CHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTIONCHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTIONNikhil Pandit
 
The scope of contribution
The scope of contributionThe scope of contribution
The scope of contributionNikhil Pandit
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend classAbhishek Wadhwa
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
4 stroke diesel engine
4 stroke diesel engine4 stroke diesel engine
4 stroke diesel engineNikhil Pandit
 
Use case Diagram and Sequence Diagram
Use case Diagram and Sequence DiagramUse case Diagram and Sequence Diagram
Use case Diagram and Sequence DiagramNikhil Pandit
 
Counterfort Retaining Wall
Counterfort Retaining WallCounterfort Retaining Wall
Counterfort Retaining WallKaizer Dave
 
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operationNikhil Pandit
 
Register transfer and micro operation
Register transfer and micro operationRegister transfer and micro operation
Register transfer and micro operationKamal Acharya
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classesasadsardar
 

Viewers also liked (20)

Diodes
DiodesDiodes
Diodes
 
Chapter23 friend-function-friend-class
Chapter23 friend-function-friend-classChapter23 friend-function-friend-class
Chapter23 friend-function-friend-class
 
Object as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younasObject as function argument , friend and static function by shahzad younas
Object as function argument , friend and static function by shahzad younas
 
Safety rules and earthing
Safety rules and earthingSafety rules and earthing
Safety rules and earthing
 
Simplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationSimplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of Computation
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Software Coding- Software Coding
Software Coding- Software CodingSoftware Coding- Software Coding
Software Coding- Software Coding
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Spyware and rootkit
Spyware and rootkitSpyware and rootkit
Spyware and rootkit
 
Inheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphismInheritance, friend function, virtual function, polymorphism
Inheritance, friend function, virtual function, polymorphism
 
CHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTIONCHAIN RULE AND IMPLICIT FUNCTION
CHAIN RULE AND IMPLICIT FUNCTION
 
The scope of contribution
The scope of contributionThe scope of contribution
The scope of contribution
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
4 stroke diesel engine
4 stroke diesel engine4 stroke diesel engine
4 stroke diesel engine
 
Use case Diagram and Sequence Diagram
Use case Diagram and Sequence DiagramUse case Diagram and Sequence Diagram
Use case Diagram and Sequence Diagram
 
Counterfort Retaining Wall
Counterfort Retaining WallCounterfort Retaining Wall
Counterfort Retaining Wall
 
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operation
 
Register transfer and micro operation
Register transfer and micro operationRegister transfer and micro operation
Register transfer and micro operation
 
Friends function and_classes
Friends function and_classesFriends function and_classes
Friends function and_classes
 

Similar to Functions in C++

Similar to Functions in C++ (20)

Function
FunctionFunction
Function
 
Function
FunctionFunction
Function
 
Function in cpu 2
Function in cpu 2Function in cpu 2
Function in cpu 2
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Function in c language(defination and declaration)
Function in c language(defination and declaration)Function in c language(defination and declaration)
Function in c language(defination and declaration)
 
Functions
FunctionsFunctions
Functions
 
C structure
C structureC structure
C structure
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Function
FunctionFunction
Function
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
5. Functions in C.pdf
5. Functions in C.pdf5. Functions in C.pdf
5. Functions in C.pdf
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
FUNCTIONS.pptx
FUNCTIONS.pptxFUNCTIONS.pptx
FUNCTIONS.pptx
 
C FUNCTIONS
C FUNCTIONSC FUNCTIONS
C FUNCTIONS
 
Functions
Functions Functions
Functions
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
functions
functionsfunctions
functions
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 

Recently uploaded

Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsResearcher Researcher
 
Structural Integrity Assessment Standards in Nigeria by Engr Nimot Muili
Structural Integrity Assessment Standards in Nigeria by Engr Nimot MuiliStructural Integrity Assessment Standards in Nigeria by Engr Nimot Muili
Structural Integrity Assessment Standards in Nigeria by Engr Nimot MuiliNimot Muili
 
tourism-management-srs_compress-software-engineering.pdf
tourism-management-srs_compress-software-engineering.pdftourism-management-srs_compress-software-engineering.pdf
tourism-management-srs_compress-software-engineering.pdfchess188chess188
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewsandhya757531
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfalene1
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Coursebim.edu.pl
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Romil Mishra
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxTriangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxRomil Mishra
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Sumanth A
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTSneha Padhiar
 
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...gerogepatton
 
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...Amil baba
 
Theory of Machine Notes / Lecture Material .pdf
Theory of Machine Notes / Lecture Material .pdfTheory of Machine Notes / Lecture Material .pdf
Theory of Machine Notes / Lecture Material .pdfShreyas Pandit
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptxmohitesoham12
 
Indian Tradition, Culture & Societies.pdf
Indian Tradition, Culture & Societies.pdfIndian Tradition, Culture & Societies.pdf
Indian Tradition, Culture & Societies.pdfalokitpathak01
 
70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical trainingGladiatorsKasper
 

Recently uploaded (20)

Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending Actuators
 
Structural Integrity Assessment Standards in Nigeria by Engr Nimot Muili
Structural Integrity Assessment Standards in Nigeria by Engr Nimot MuiliStructural Integrity Assessment Standards in Nigeria by Engr Nimot Muili
Structural Integrity Assessment Standards in Nigeria by Engr Nimot Muili
 
tourism-management-srs_compress-software-engineering.pdf
tourism-management-srs_compress-software-engineering.pdftourism-management-srs_compress-software-engineering.pdf
tourism-management-srs_compress-software-engineering.pdf
 
Artificial Intelligence in Power System overview
Artificial Intelligence in Power System overviewArtificial Intelligence in Power System overview
Artificial Intelligence in Power System overview
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
 
Versatile Engineering Construction Firms
Versatile Engineering Construction FirmsVersatile Engineering Construction Firms
Versatile Engineering Construction Firms
 
ASME-B31.4-2019-estandar para diseño de ductos
ASME-B31.4-2019-estandar para diseño de ductosASME-B31.4-2019-estandar para diseño de ductos
ASME-B31.4-2019-estandar para diseño de ductos
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptxTriangulation survey (Basic Mine Surveying)_MI10412MI.pptx
Triangulation survey (Basic Mine Surveying)_MI10412MI.pptx
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
 
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENTFUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
FUNCTIONAL AND NON FUNCTIONAL REQUIREMENT
 
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...
March 2024 - Top 10 Read Articles in Artificial Intelligence and Applications...
 
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...
Uk-NO1 kala jadu karne wale ka contact number kala jadu karne wale baba kala ...
 
Theory of Machine Notes / Lecture Material .pdf
Theory of Machine Notes / Lecture Material .pdfTheory of Machine Notes / Lecture Material .pdf
Theory of Machine Notes / Lecture Material .pdf
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
Indian Tradition, Culture & Societies.pdf
Indian Tradition, Culture & Societies.pdfIndian Tradition, Culture & Societies.pdf
Indian Tradition, Culture & Societies.pdf
 
70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training70 POWER PLANT IAE V2500 technical training
70 POWER PLANT IAE V2500 technical training
 

Functions in C++

  • 1. GANDHINAGAR INSTITUTE OF TECHNOLOGY Department of Computer Engineering Function in C++
  • 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. 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.
  • 10. 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; }
  • 11. Calculating the Area of a Circle with a Function
  • 12. #include<iostream.h> #include<conio.h> const float pi=3.14; float area(float r) //  This is the function for calculating area { float ar; ar=pi*r*r; return ar; } void main() { float r,result; clrscr(); cout<<“nEnter the Radius of Circle: n”; cin>>r; result=area(r); //  Function call cout<<“nArea of Circle: “<<result<<endl; getch(); }