SlideShare a Scribd company logo
Functions
C++ lecture 03
Functions
 Functions allow to structure programs in segments of code to
perform individual tasks.
 It is a group of statements that is given a name, and which can be
called from some point of the program.
 The most common syntax to define a function is:
type name ( parameter1, parameter2, ...)
{
statements
}
 type - the type of the value returned by the
function.
 Name - the identifier by which the function can be
called.
 Parameters -The purpose of parameters is to allow
passing arguments to the function from the location
where it is called from.
 Statements - the function's body. It is a block of
statements surrounded by braces { } that specify
what the function actually does.
Functions
Example
int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main () {
int z;
z = addition (5,3);
cout << "The result is " << z;
}
Type
Name
Parameter
Statements
Explanation
 The final statement within the function:
return r;
int subtraction (int a, int b) {
int r;
r=a-b;
return r;
}
int main () {
int x=5, y=3, z;
z = subtraction (7,2);
cout << "The first result is " << z << 'n';
cout << "The second result is " << subtraction
(7,2) << 'n';
cout << "The third result is " << subtraction
(x,y) << 'n';
z= 4 + subtraction (x,y);
cout << "The fourth result is " << z << 'n'; }
Functions with no return type
// void function example
#include <iostream>
using namespace std;
void printmessage () {
cout << "I'm a function!";
}
int main () {
printmessage ();
}
Arguments passed by value
 when calling a function, the values of the arguments on
the moment of the call, which are copied into the variables
represented by the function parameters.
 For example, take:
int x=5, y=3, z;
z = addition ( x, y );
Arguments passed by
reference
 Passing by reference refers to a method of passing
arguments where the value of an argument in the
calling function can be modified in the called
function.
 It allows to access an external variable from within
a function
passing parameters by reference
#include <iostream>
using namespace std;
void duplicate (int& a, int& b, int& c) {
a*=2;
b*=2;
c*=2;
}
int main () {
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
Explanation
 When a variable is passed by reference, what is passed is no
longer a copy
 the variable itself, identified by the function parameter
associated with the argument passed to the function
 any modification on local variables within the function are
reflected in the variables passed as arguments in the call
Declaration
 If instead of defining duplicate as:
 void duplicate (int& a, int& b, int& c)
 Was it to be defined without the ampersand signs as:
void duplicate (int a, int b, int c)
 The variables would not be passed by reference, but by
value, creating instead copies of their values.
 In this case, the output of the program would have
been the values of x, y, and z without being modified
(i.e., 1, 3, and 7)
Recursive Functions
 Recursive is the property that functions have to be
called by themselves
 n! = n * (n-1) * (n-2) * (n-3) ... * 1
 5! = 5 * 4 * 3 * 2 * 1 = 120
while ( i <= number) {
Factorial = Factorial * i;
++i;
}
Recursive Function
int factorial (int a) {
if (a > 1)
return (a * factorial (a-1));
else
return 1;
}
C++ Standard Library
 Refer the following website and find the C++ standard
header files and inbuilt functions.
 www.cplusplus.com/reference/

More Related Content

PPTX
Functions in C
PPSX
Functions in c
PPT
Lecture#7 Call by value and reference in c++
PPTX
Presentation on function
PPTX
Function in c
PPTX
Call by value or call by reference in C++
PPT
lets play with "c"..!!! :):)
Functions in C
Functions in c
Lecture#7 Call by value and reference in c++
Presentation on function
Function in c
Call by value or call by reference in C++
lets play with "c"..!!! :):)

What's hot (20)

PPTX
parameter passing in c#
PPTX
Recursion in c++
PPT
Function overloading(C++)
PPT
C++ Function
DOCX
Maharishi University of Management (MSc Computer Science test questions)
PPT
Function in C Language
PPTX
Function in c program
PPTX
Function in c language(defination and declaration)
PPTX
C function
PDF
C standard library functions
PPTX
This pointer
PPTX
Function in C program
PPSX
Function in c
PPTX
Function Pointer
PPTX
C and C++ functions
PPTX
functions in C
PPTX
PDF
03 function overloading
PPTX
Working with functions in matlab
PDF
Functions
parameter passing in c#
Recursion in c++
Function overloading(C++)
C++ Function
Maharishi University of Management (MSc Computer Science test questions)
Function in C Language
Function in c program
Function in c language(defination and declaration)
C function
C standard library functions
This pointer
Function in C program
Function in c
Function Pointer
C and C++ functions
functions in C
03 function overloading
Working with functions in matlab
Functions
Ad

Viewers also liked (20)

PPTX
C++ lecture 04
PPTX
C++ lecture 01
PPTX
C++ lecture 02
PPTX
Lecture 02 networking
PPTX
Lecture 01 networking
PDF
Netw450 advanced network security with lab entire class
PDF
C++ L05-Functions
PPTX
Functions c++ مشروع
PPT
Functions in C++
PDF
Network-security muhibullah aman-first edition-in Persian
PDF
Network Security in 2016
PPT
Functions in c++
PPTX
Learning C++ - Functions in C++ 3
DOCX
The Algebra of Functions
PDF
Basic openCV Functions Using CPP
PPTX
Functions in C++
PPTX
C++ ppt
PDF
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
PPT
Functions in c++
PPTX
C++ Programming Language Training in Ambala ! Batra Computer Centre
C++ lecture 04
C++ lecture 01
C++ lecture 02
Lecture 02 networking
Lecture 01 networking
Netw450 advanced network security with lab entire class
C++ L05-Functions
Functions c++ مشروع
Functions in C++
Network-security muhibullah aman-first edition-in Persian
Network Security in 2016
Functions in c++
Learning C++ - Functions in C++ 3
The Algebra of Functions
Basic openCV Functions Using CPP
Functions in C++
C++ ppt
Web Applications Under Attack: Why Network Security Solutions Leave You Exposed
Functions in c++
C++ Programming Language Training in Ambala ! Batra Computer Centre
Ad

Similar to C++ lecture 03 (20)

PPTX
UNIT3.pptx
DOC
Functions
PPTX
Dti2143 chapter 5
PPTX
C Programming Language Part 7
PPT
Inbuilt Functions in C++ computer language.ppt
PPTX
Amit user defined functions xi (2)
PPT
Fp201 unit5 1
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
PPTX
Classes function overloading
PPTX
functions of C++
PPTX
C++ FUNCTIONS-1.pptx
PPTX
functions
DOC
Unit 4 (1)
PPT
Lecture 5
PPT
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
PPT
function_v1fgdfdf5645ythyth6ythythgbg.ppt
PDF
FUNCTIONS IN C PROGRAMMING.pdf
PPTX
Detailed concept of function in c programming
PDF
46630497 fun-pointer-1
ODP
Function
UNIT3.pptx
Functions
Dti2143 chapter 5
C Programming Language Part 7
Inbuilt Functions in C++ computer language.ppt
Amit user defined functions xi (2)
Fp201 unit5 1
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Classes function overloading
functions of C++
C++ FUNCTIONS-1.pptx
functions
Unit 4 (1)
Lecture 5
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
function_v1fgdfdf5645ythyth6ythythgbg.ppt
FUNCTIONS IN C PROGRAMMING.pdf
Detailed concept of function in c programming
46630497 fun-pointer-1
Function

Recently uploaded (20)

PPTX
Geodesy 1.pptx...............................................
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PDF
Well-logging-methods_new................
PDF
Digital Logic Computer Design lecture notes
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Welding lecture in detail for understanding
PPTX
Construction Project Organization Group 2.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Sustainable Sites - Green Building Construction
PDF
PPT on Performance Review to get promotions
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Geodesy 1.pptx...............................................
Strings in CPP - Strings in C++ are sequences of characters used to store and...
Well-logging-methods_new................
Digital Logic Computer Design lecture notes
UNIT-1 - COAL BASED THERMAL POWER PLANTS
bas. eng. economics group 4 presentation 1.pptx
Welding lecture in detail for understanding
Construction Project Organization Group 2.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Sustainable Sites - Green Building Construction
PPT on Performance Review to get promotions
OOP with Java - Java Introduction (Basics)
Foundation to blockchain - A guide to Blockchain Tech
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Model Code of Practice - Construction Work - 21102022 .pdf
UNIT 4 Total Quality Management .pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...

C++ lecture 03

  • 2. Functions  Functions allow to structure programs in segments of code to perform individual tasks.  It is a group of statements that is given a name, and which can be called from some point of the program.  The most common syntax to define a function is: type name ( parameter1, parameter2, ...) { statements }
  • 3.  type - the type of the value returned by the function.  Name - the identifier by which the function can be called.  Parameters -The purpose of parameters is to allow passing arguments to the function from the location where it is called from.  Statements - the function's body. It is a block of statements surrounded by braces { } that specify what the function actually does. Functions
  • 4. Example int addition (int a, int b) { int r; r=a+b; return r; } int main () { int z; z = addition (5,3); cout << "The result is " << z; } Type Name Parameter Statements
  • 5. Explanation  The final statement within the function: return r;
  • 6. int subtraction (int a, int b) { int r; r=a-b; return r; } int main () { int x=5, y=3, z; z = subtraction (7,2); cout << "The first result is " << z << 'n'; cout << "The second result is " << subtraction (7,2) << 'n'; cout << "The third result is " << subtraction (x,y) << 'n'; z= 4 + subtraction (x,y); cout << "The fourth result is " << z << 'n'; }
  • 7. Functions with no return type // void function example #include <iostream> using namespace std; void printmessage () { cout << "I'm a function!"; } int main () { printmessage (); }
  • 8. Arguments passed by value  when calling a function, the values of the arguments on the moment of the call, which are copied into the variables represented by the function parameters.  For example, take: int x=5, y=3, z; z = addition ( x, y );
  • 9. Arguments passed by reference  Passing by reference refers to a method of passing arguments where the value of an argument in the calling function can be modified in the called function.  It allows to access an external variable from within a function
  • 10. passing parameters by reference #include <iostream> using namespace std; void duplicate (int& a, int& b, int& c) { a*=2; b*=2; c*=2; } int main () { int x=1, y=3, z=7; duplicate (x, y, z); cout << "x=" << x << ", y=" << y << ", z=" << z; return 0; }
  • 11. Explanation  When a variable is passed by reference, what is passed is no longer a copy  the variable itself, identified by the function parameter associated with the argument passed to the function  any modification on local variables within the function are reflected in the variables passed as arguments in the call
  • 12. Declaration  If instead of defining duplicate as:  void duplicate (int& a, int& b, int& c)  Was it to be defined without the ampersand signs as: void duplicate (int a, int b, int c)  The variables would not be passed by reference, but by value, creating instead copies of their values.  In this case, the output of the program would have been the values of x, y, and z without being modified (i.e., 1, 3, and 7)
  • 13. Recursive Functions  Recursive is the property that functions have to be called by themselves  n! = n * (n-1) * (n-2) * (n-3) ... * 1  5! = 5 * 4 * 3 * 2 * 1 = 120 while ( i <= number) { Factorial = Factorial * i; ++i; }
  • 14. Recursive Function int factorial (int a) { if (a > 1) return (a * factorial (a-1)); else return 1; }
  • 15. C++ Standard Library  Refer the following website and find the C++ standard header files and inbuilt functions.  www.cplusplus.com/reference/