Functions in c++
09/25/202
4
By MRI
1
Functions in C++
Functions are used to provide modularity to a
program. Creating an application using
function makes it easier to understand, edit,
check errors etc.
Functions allow to structure programs in
segments of code to perform individual tasks.
In C++, a function is a group of statements
that is given a name, and which can be
called from some point of the program.
09/25/202
4
By MRI
2
Functions in C++
Depending on whether a function is
predefined or created by programmer; there
are two types of function:
1. Library Function OR Built in function
2. User-defined Function
09/25/202
4
By MRI
3
09/25/202
4
Types
1.Built in functions :-
are part of compiler package.
Part of standard library made available by compiler.
Can be used in any program by including respective header file.
2. User defined functions:-
Created by user or programmer.
Created as per requirement of
the program.
User Defined Functions in C++
Syntax of Function
return-type function-name (parameters)
{
// function-body
}
return-type : suggests what the function will return. It can be int,
char, some pointer or even a class object. There can be functions
which does not return anything, they are mentioned with void.
Function Name : is the name of the function, using the function
name it is called.
Parameters : are variables to hold values of arguments passed
while function is called. A function may or may not contain
parameter list.
09/25/202
4
By MRI
5
09/25/202
4
6 Parts of a function
main function
{
function prototype declaration
function call
-------;
}
function declaratory/definition
{
------;
return statement
}
09/25/202
4
By MRI
7
Function prototype
4 parts
i. Return type
ii. Function name
iii. Argument list
iv. Terminating semicolon
Variable declaration
Data_type variable_name ;
int x=5;
float marks;
int price;
A function prototype is a declaration of a function that tells
the program about the type of value returned by the
function, name of function, number and type of
arguments.
Syntax: Return_type function_name (parameter
list/argument);
EX: int add(int,int);
void add(void);
int add(float,int);
Declaring, Defining and Calling Function
#include < iostream>
using namespace std;
int sum (int x, int y); //declaring function
int main()
{
int a = 10;
int b = 20;
int c = sum (a, b); //calling function
cout << c;
}
int sum (int x, int y) //defining function
{
return (X + y);
}
09/25/202
4
By MRI
8
Function definition
Syntax: function_type function_name(parameter
list) {
Local variable
declaration; Function
body statement; Return
statement;
}
Function
body
2 parts
Function
header
Note: no semicolon in
header
Example: int add(int,int);
Z=add(x,y);
//prototype
//function call
//function definition
int add(int a,int b)
{
body statement;
Return(a+b);
P.P.Krishnara
}j RSET
P.P.Krishnaraj
RSET
Function
categories
i) Function with no return value and no argument.
void add(void);
ii) Function with arguments passed and no return value.
void add(int,int);
iii) Function with no arguments but returns a value.
int add(void);
iv) Function with arguments and returns a value.
int add(int,int);
I. Function with no return value and no argument
void main()
{
void disp(void);
//prototype //caller function
disp();
return 0;
}
void disp() //calle function
{
cout<<“--------”<<endl;
}
No arguments passed
from caller to calle
No value returned from
calle to caller function
P.P.Krishnaraj
RSET
P.P.Krishnaraj
RSET
//program to print square of a number using functions.
void main()
{
void sqr(void);
sqr();
getch();
return 0;
}
void
sqr()
{
int no;
cout<<“enter a no.”;
cin>>no;
cout<<“square
of”<<no<<“is”<<no
*no;
}
ii. Function will not return any value but passes argument
#include<iostream.h>
#include<conio.h>
void add(int,int);
int main()
{
int a,b;
cout<<“enter values of a and b”<<endl;
cin>>a>>b;
add(a,b);
getch();
return 0;
}
void
add(int
x,int y)
{
int c;
c=x+y;
cout<<
“additi
on
is”<<c
;
add(a,b);
a
b
void add(int x,int y);
P.P.Krishnaraj
RSET
iii) Function with arguments and return value
main function
{
int sqr(int);
int a,ans;
cout<<“enter a number”;
cin>>a;
ans=sqr(a);
cout<<“square of number is”<<ans;
getch();
return 0;
}
//function prototype
//function call
int sqr(int X)
{
return(X*X);
}
//function declaratory/definition
P.P.Krishnaraj
RSET
iv) Function with no arguments but returns a value
int main()
{
int add(void);
int z;
z=add();
cout<<sum
of 2 nos
is”<<z;
getch();
return 0;
}
int add(void);
{
int a,b;
cout<<“enter 2 nos”;
cin>>a>>b;
return(a+b);
}
Function call
add(x,y);
i.e z=add(x,y);
P.P.Krishnaraj
RSET
Calling a Function
Functions are called by their names. If the function is
without argument, it can be called directly using its
name. But for functions with arguments, we have two
ways to call them:
1. Call by Value
2. Call by Reference
09/25/202
4
By MRI
16
Call by value
A function can be invoked in two
manners (i)call by value
(ii)call by reference
The call by value method copies the value of actual parameters into
formal parameters i.e the function creates its own copy of
arguments and uses them.
add(a,b); Values of variables a
and b are passed to
X,Y
Now if you change
the value of X and Y
,
those changes are
not seen in a and b
Call by value
where the values of
variable are passed
to functions
}
void add(int x,int y);
{
--------;
P.P.Kris}hnaraj RSET
/* program to illustrate the concept of call
by value */
#include<iostream.h
#include<conio.h> void
add(int,int);
int main()
{
int a,b;
cout<<“enter values of a and b”<<endl;
cin>>a>>b;
add(a,b);
getch(); return 0;
}
void add(int x,int
y)
{
int c; c=x+y
cout<<“addition is”<<c;
}
Call by reference
In call by reference method in place of calling a value to the function being
called , a reference to the original variable is passed .i.e the same variable
value can be accessed by any of the two names.
add(a,b);
}
P.P.Krishnaraj
RSET
void add(int &x,int &y);
{
--------;
}
In function call
We write
reference variable
for formal
arguments
&X,&Y will be the
reference variable for
a and b. if we
change X and Y,
Value of a and b are
changed accordingly
No need for return
statement
Program to illustrate call by reference
#include<iostream.h>
#include<conio.h>
void swap(int &,int
&); int main()
{
int a,b;
cout<<“enter the values of a and
b”; cin>>a>>b;
cout<<“before
swaping”;
cout<<“A”<<a;
cout<<“B”<<b;
swap(a,b);
cout<<“after swaping”;
cout<<“A”<<a;
cout<<“B”<<b;
getch();
}
void swap(int &X,&Y)
{
int temp;
temp=X;
X=Y;
Y=X;
}
P.P.Krishnaraj RSET
Inline function
Calling a function generally causes a certain
overhead (stacking arguments, jumps,
etc...), and thus for very short functions, it
may be more efficient to simply insert the
code of the function where it is called,
instead of performing the process of formally
calling a function.
09/25/202
4
By MRI
21
Inline function
Preceding a function declaration with the
inline specifier informs the compiler that
inline expansion is preferred over the usual
function call mechanism for a specific
function.
This does not change at all the behavior of a
function, but is merely used to suggest the
compiler that the code generated by the
function body shall be inserted at each
point the function is called, instead of being
invoked with a regular function call.
09/25/202
4
By MRI
22
Inline function
C++ inline function is powerful concept that is
commonly used with classes. If a function is
inline, the compiler places a copy of the
code of that function at each point where
the function is called at compile time.
To inline a function, place the
keyword inline before the function name
and define the function before any calls are
made to the function.
The compiler can ignore the inline qualifier in
case defined function is more than a line.
09/25/202
4
By MRI
23
Inline function
Following is an example, which makes use of
inline function to return max of two numbers:
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{ return (x > y)? x : y; }
// Main function for the program
int main( ) {
cout << "Max (20,10): " << Max(20,10) << endl; cout <<
"Max (0,200): " << Max(0,200) << endl; cout << "Max
(100,1010): " << Max(100,1010) << endl; return 0;
}
09/25/202
4
By MRI
24
Friend function
A friend function of a class is defined outside
that class' scope but it has the right to
access all private and protected members
of the class. Even though the prototypes for
friend functions appear in the class
definition, friends are not member functions.
A friend can be a function, function template,
or member function, or a class or class
template, in which case the entire class and
all of its members are friends.
09/25/202
4
By MRI
25
Friend function
To declare a function as a friend of a class,
precede the function prototype in the class
definition with keyword friend as follows:
class Box {
double width;
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};
09/25/202
4
By MRI
26
Friend function
#include <iostream>
using namespace std;
// forward declaration
class B;
class A
{
private:
int numA;
public:
A(): numA(12) { } // friend function declaration
friend int add(A, B);
};
class B {
private:
int numB;
public:
B(): numB(1) { } // friend function declaration
friend int add(A , B);
};
// Function add() is the friend function of classes A and B
// that accesses the member variables numA and numB
int add(A objectA, B objectB)
{
return (objectA.numA + objectB.numB);
}
int main()
{ A objectA;
B objectB;
cout<<"Sum: "<< add(objectA, objectB);
return 0;
}
09/25/202
4
By MRI
27
Friend function
In this program, classes A and B have
declared add() as a friend function.
Thus, this function can access private data of
both class.
Here, add() function adds the private data
numA and numB of two objects objectA and
objectB, and returns it to the main function.
To make this program work properly, a forward
declaration of a class class B should be made
as shown in the above example.
This is because class B is referenced within the
class A using code: friend int add(A , B);.
09/25/202
4
By MRI
28
Recursive function
A function that calls itself is known as recursive
function. And, this technique is known as
recursion.
void recurse()
{
... .. ...
recurse();
... .. ...
}
int main()
{
... .. ...
recurse();
... .. ...
}
09/25/202
4
By MRI
29
09/25/202
4
By MRI
30
Examples of Recursive Function
// Factorial of n = 1*2*3*...*n
#include <iostream>
using namespace std;
int factorial(int);
int main()
{
int n;
cout<<"Enter a number to find factorial: ";
cin >> n;
cout << "Factorial of " << n <<" = " << factorial(n);
return 0;
}
int factorial(int n)
{
if (n > 1)
{
return n*factorial(n-1);
}
else
{
return 1;
}
}
09/25/202
4
By MRI
31

More Related Content

PPTX
Functions in c++
PPTX
Functions1
PDF
Functions in C++
PPTX
Functions in C++
PPTX
FUNCTIONS, CLASSES AND OBJECTS.pptx
PPT
PPTX
Chapter One Function.pptx
PPTX
C++ Functions.pptx
Functions in c++
Functions1
Functions in C++
Functions in C++
FUNCTIONS, CLASSES AND OBJECTS.pptx
Chapter One Function.pptx
C++ Functions.pptx

Similar to functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx (20)

PPT
16717 functions in C++
 
PDF
PPTX
Functions and modular programming.pptx
PPTX
Silde of the cse fundamentals a deep analysis
PPT
User Defined Functions
PDF
Function overloading ppt
PPTX
Chapter 4
PPTX
Functions in C++ (OOP)
PDF
Functions in C++.pdf
PDF
Chapter 11 Function
PPT
Chapter 1.ppt
DOCX
Functions assignment
PPTX
Amit user defined functions xi (2)
PPT
Chapter Introduction to Modular Programming.ppt
PPT
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
PPTX
Function in C++, Methods in C++ coding programming
PPTX
Functions in C++
PDF
C++ Object oriented concepts & programming
PPTX
C++_Functions_Detailed_Presentation.pptx
PDF
Cpp functions
16717 functions in C++
 
Functions and modular programming.pptx
Silde of the cse fundamentals a deep analysis
User Defined Functions
Function overloading ppt
Chapter 4
Functions in C++ (OOP)
Functions in C++.pdf
Chapter 11 Function
Chapter 1.ppt
Functions assignment
Amit user defined functions xi (2)
Chapter Introduction to Modular Programming.ppt
chapterintroductiontomodularprogramming-230112092330-e3eb5a74 (1).ppt
Function in C++, Methods in C++ coding programming
Functions in C++
C++ Object oriented concepts & programming
C++_Functions_Detailed_Presentation.pptx
Cpp functions
Ad

More from nandemprasanna (18)

PPTX
CSS propertievcvcbccccbvcccccvcbvcvbcs.pptx
PPT
RADRAD1RAD1RAD1RAD1RAD1RAD1RAD1RAD1RAD1.ppt
PPTX
1707325642974_Classes fffand objects.pptx
PPTX
ilovepdf_mergebvhfhbxcbcgsdgsdvcxcvd.pptx
PPT
LECT3A (1).PPThhdfghdfhdfghdhdhdfsfdfgsfd
PPT
4_25655_SE291_2020_1__2_1_Lecture 3 - Software Process Models (1).ppt
PPT
Intro to Multimediartgtwertrwtwetwttw.ppt
PPTX
Multimedia.pptxgdsssssssssssssssssssssssssssss
PPT
cpp class unitdfdsfasadfsdASsASass 4.ppt
PPTX
Internet programming computers presentation.pptx
PPTX
versionsofwindows1-20000000413054842.pptx
PPTX
MSWORD programming computers fundamentales.pptx
PPT
COMPUTERS STRUCTURES OS YHE Sblock diagram.ppt
PPTX
introductiontocprogramming datatypespp.pptx
PPT
datatypesinformation datatypes finin5.ppt
PPTX
Desktop Publishing Example basics presentation
PPT
Computer-Networks,types of lan wan manNetwork.ppt
PPTX
operating system basic and windows versions and types of operating system
CSS propertievcvcbccccbvcccccvcbvcvbcs.pptx
RADRAD1RAD1RAD1RAD1RAD1RAD1RAD1RAD1RAD1.ppt
1707325642974_Classes fffand objects.pptx
ilovepdf_mergebvhfhbxcbcgsdgsdvcxcvd.pptx
LECT3A (1).PPThhdfghdfhdfghdhdhdfsfdfgsfd
4_25655_SE291_2020_1__2_1_Lecture 3 - Software Process Models (1).ppt
Intro to Multimediartgtwertrwtwetwttw.ppt
Multimedia.pptxgdsssssssssssssssssssssssssssss
cpp class unitdfdsfasadfsdASsASass 4.ppt
Internet programming computers presentation.pptx
versionsofwindows1-20000000413054842.pptx
MSWORD programming computers fundamentales.pptx
COMPUTERS STRUCTURES OS YHE Sblock diagram.ppt
introductiontocprogramming datatypespp.pptx
datatypesinformation datatypes finin5.ppt
Desktop Publishing Example basics presentation
Computer-Networks,types of lan wan manNetwork.ppt
operating system basic and windows versions and types of operating system
Ad

Recently uploaded (20)

PDF
Clay-Unearthing-its-Mysteries for clay ceramics and glass molding
PDF
analisis snsistem etnga ahrfahfffffffffffffffffffff
PPTX
Presentation.pptx anemia in pregnancy in
PPTX
8086.pptx microprocessor and microcontroller
PPT
416170345656655446879265596558865588.ppt
PDF
Humans do not die they live happily without
PDF
Engineering drawing lecture no 2 building blocks
PPTX
UNITy8 human computer interac5ion-1.pptx
PDF
Control and coordination isdorjdmdndjke
PPT
failures in f pd.ppt
PPTX
Drafting equipment and its care for interior design
PPTX
3 - Meeting Life Challengjrh89wyrhnadiurhjdsknhfueihru
PDF
Pfthuujhgdddtyygghjjiuyggghuiiiijggbbhhh
PDF
Instagram Marketing in 2025 Reels, Stories, and Strategy (14) (2).pdf
PDF
Kindly check my updated curriculum Vitae
PDF
jyg7ur7rtb7ur57vr65r7t7b7i6t7r65rb57t76bt
PDF
321 LIBRARY DESIGN.pdf43354445t6556t5656
PDF
trenching-standard-drawings procedure rev
PPTX
Applied Anthropology Report.pptx paulapuhin
PPTX
Project_Presentation Bitcoin Price Prediction
Clay-Unearthing-its-Mysteries for clay ceramics and glass molding
analisis snsistem etnga ahrfahfffffffffffffffffffff
Presentation.pptx anemia in pregnancy in
8086.pptx microprocessor and microcontroller
416170345656655446879265596558865588.ppt
Humans do not die they live happily without
Engineering drawing lecture no 2 building blocks
UNITy8 human computer interac5ion-1.pptx
Control and coordination isdorjdmdndjke
failures in f pd.ppt
Drafting equipment and its care for interior design
3 - Meeting Life Challengjrh89wyrhnadiurhjdsknhfueihru
Pfthuujhgdddtyygghjjiuyggghuiiiijggbbhhh
Instagram Marketing in 2025 Reels, Stories, and Strategy (14) (2).pdf
Kindly check my updated curriculum Vitae
jyg7ur7rtb7ur57vr65r7t7b7i6t7r65rb57t76bt
321 LIBRARY DESIGN.pdf43354445t6556t5656
trenching-standard-drawings procedure rev
Applied Anthropology Report.pptx paulapuhin
Project_Presentation Bitcoin Price Prediction

functIONS PROGRAMMING CIII II DJDJKASDJKJASD.pptx

  • 2. Functions in C++ Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, check errors etc. Functions allow to structure programs in segments of code to perform individual tasks. In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. 09/25/202 4 By MRI 2
  • 3. Functions in C++ Depending on whether a function is predefined or created by programmer; there are two types of function: 1. Library Function OR Built in function 2. User-defined Function 09/25/202 4 By MRI 3
  • 4. 09/25/202 4 Types 1.Built in functions :- are part of compiler package. Part of standard library made available by compiler. Can be used in any program by including respective header file. 2. User defined functions:- Created by user or programmer. Created as per requirement of the program.
  • 5. User Defined Functions in C++ Syntax of Function return-type function-name (parameters) { // function-body } return-type : suggests what the function will return. It can be int, char, some pointer or even a class object. There can be functions which does not return anything, they are mentioned with void. Function Name : is the name of the function, using the function name it is called. Parameters : are variables to hold values of arguments passed while function is called. A function may or may not contain parameter list. 09/25/202 4 By MRI 5
  • 6. 09/25/202 4 6 Parts of a function main function { function prototype declaration function call -------; } function declaratory/definition { ------; return statement }
  • 7. 09/25/202 4 By MRI 7 Function prototype 4 parts i. Return type ii. Function name iii. Argument list iv. Terminating semicolon Variable declaration Data_type variable_name ; int x=5; float marks; int price; A function prototype is a declaration of a function that tells the program about the type of value returned by the function, name of function, number and type of arguments. Syntax: Return_type function_name (parameter list/argument); EX: int add(int,int); void add(void); int add(float,int);
  • 8. Declaring, Defining and Calling Function #include < iostream> using namespace std; int sum (int x, int y); //declaring function int main() { int a = 10; int b = 20; int c = sum (a, b); //calling function cout << c; } int sum (int x, int y) //defining function { return (X + y); } 09/25/202 4 By MRI 8
  • 9. Function definition Syntax: function_type function_name(parameter list) { Local variable declaration; Function body statement; Return statement; } Function body 2 parts Function header Note: no semicolon in header Example: int add(int,int); Z=add(x,y); //prototype //function call //function definition int add(int a,int b) { body statement; Return(a+b); P.P.Krishnara }j RSET
  • 10. P.P.Krishnaraj RSET Function categories i) Function with no return value and no argument. void add(void); ii) Function with arguments passed and no return value. void add(int,int); iii) Function with no arguments but returns a value. int add(void); iv) Function with arguments and returns a value. int add(int,int);
  • 11. I. Function with no return value and no argument void main() { void disp(void); //prototype //caller function disp(); return 0; } void disp() //calle function { cout<<“--------”<<endl; } No arguments passed from caller to calle No value returned from calle to caller function P.P.Krishnaraj RSET
  • 12. P.P.Krishnaraj RSET //program to print square of a number using functions. void main() { void sqr(void); sqr(); getch(); return 0; } void sqr() { int no; cout<<“enter a no.”; cin>>no; cout<<“square of”<<no<<“is”<<no *no; }
  • 13. ii. Function will not return any value but passes argument #include<iostream.h> #include<conio.h> void add(int,int); int main() { int a,b; cout<<“enter values of a and b”<<endl; cin>>a>>b; add(a,b); getch(); return 0; } void add(int x,int y) { int c; c=x+y; cout<< “additi on is”<<c ; add(a,b); a b void add(int x,int y); P.P.Krishnaraj RSET
  • 14. iii) Function with arguments and return value main function { int sqr(int); int a,ans; cout<<“enter a number”; cin>>a; ans=sqr(a); cout<<“square of number is”<<ans; getch(); return 0; } //function prototype //function call int sqr(int X) { return(X*X); } //function declaratory/definition P.P.Krishnaraj RSET
  • 15. iv) Function with no arguments but returns a value int main() { int add(void); int z; z=add(); cout<<sum of 2 nos is”<<z; getch(); return 0; } int add(void); { int a,b; cout<<“enter 2 nos”; cin>>a>>b; return(a+b); } Function call add(x,y); i.e z=add(x,y); P.P.Krishnaraj RSET
  • 16. Calling a Function Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them: 1. Call by Value 2. Call by Reference 09/25/202 4 By MRI 16
  • 17. Call by value A function can be invoked in two manners (i)call by value (ii)call by reference The call by value method copies the value of actual parameters into formal parameters i.e the function creates its own copy of arguments and uses them. add(a,b); Values of variables a and b are passed to X,Y Now if you change the value of X and Y , those changes are not seen in a and b Call by value where the values of variable are passed to functions } void add(int x,int y); { --------; P.P.Kris}hnaraj RSET
  • 18. /* program to illustrate the concept of call by value */ #include<iostream.h #include<conio.h> void add(int,int); int main() { int a,b; cout<<“enter values of a and b”<<endl; cin>>a>>b; add(a,b); getch(); return 0; } void add(int x,int y) { int c; c=x+y cout<<“addition is”<<c; }
  • 19. Call by reference In call by reference method in place of calling a value to the function being called , a reference to the original variable is passed .i.e the same variable value can be accessed by any of the two names. add(a,b); } P.P.Krishnaraj RSET void add(int &x,int &y); { --------; } In function call We write reference variable for formal arguments &X,&Y will be the reference variable for a and b. if we change X and Y, Value of a and b are changed accordingly No need for return statement
  • 20. Program to illustrate call by reference #include<iostream.h> #include<conio.h> void swap(int &,int &); int main() { int a,b; cout<<“enter the values of a and b”; cin>>a>>b; cout<<“before swaping”; cout<<“A”<<a; cout<<“B”<<b; swap(a,b); cout<<“after swaping”; cout<<“A”<<a; cout<<“B”<<b; getch(); } void swap(int &X,&Y) { int temp; temp=X; X=Y; Y=X; } P.P.Krishnaraj RSET
  • 21. Inline function Calling a function generally causes a certain overhead (stacking arguments, jumps, etc...), and thus for very short functions, it may be more efficient to simply insert the code of the function where it is called, instead of performing the process of formally calling a function. 09/25/202 4 By MRI 21
  • 22. Inline function Preceding a function declaration with the inline specifier informs the compiler that inline expansion is preferred over the usual function call mechanism for a specific function. This does not change at all the behavior of a function, but is merely used to suggest the compiler that the code generated by the function body shall be inserted at each point the function is called, instead of being invoked with a regular function call. 09/25/202 4 By MRI 22
  • 23. Inline function C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line. 09/25/202 4 By MRI 23
  • 24. Inline function Following is an example, which makes use of inline function to return max of two numbers: #include <iostream> using namespace std; inline int Max(int x, int y) { return (x > y)? x : y; } // Main function for the program int main( ) { cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; } 09/25/202 4 By MRI 24
  • 25. Friend function A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends. 09/25/202 4 By MRI 25
  • 26. Friend function To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows: class Box { double width; public: double length; friend void printWidth( Box box ); void setWidth( double wid ); }; 09/25/202 4 By MRI 26
  • 27. Friend function #include <iostream> using namespace std; // forward declaration class B; class A { private: int numA; public: A(): numA(12) { } // friend function declaration friend int add(A, B); }; class B { private: int numB; public: B(): numB(1) { } // friend function declaration friend int add(A , B); }; // Function add() is the friend function of classes A and B // that accesses the member variables numA and numB int add(A objectA, B objectB) { return (objectA.numA + objectB.numB); } int main() { A objectA; B objectB; cout<<"Sum: "<< add(objectA, objectB); return 0; } 09/25/202 4 By MRI 27
  • 28. Friend function In this program, classes A and B have declared add() as a friend function. Thus, this function can access private data of both class. Here, add() function adds the private data numA and numB of two objects objectA and objectB, and returns it to the main function. To make this program work properly, a forward declaration of a class class B should be made as shown in the above example. This is because class B is referenced within the class A using code: friend int add(A , B);. 09/25/202 4 By MRI 28
  • 29. Recursive function A function that calls itself is known as recursive function. And, this technique is known as recursion. void recurse() { ... .. ... recurse(); ... .. ... } int main() { ... .. ... recurse(); ... .. ... } 09/25/202 4 By MRI 29
  • 31. Examples of Recursive Function // Factorial of n = 1*2*3*...*n #include <iostream> using namespace std; int factorial(int); int main() { int n; cout<<"Enter a number to find factorial: "; cin >> n; cout << "Factorial of " << n <<" = " << factorial(n); return 0; } int factorial(int n) { if (n > 1) { return n*factorial(n-1); } else { return 1; } } 09/25/202 4 By MRI 31