The document discusses functions in C++. It defines what a function is, how functions are declared and defined, and how they are called. It covers function prototypes, formal and actual arguments, and different ways functions can be defined based on whether they have arguments and return values. It also discusses passing arguments by value, address, and reference, and covers default arguments, inline functions, and const arguments.
1. Prepared By: Asst. Prof. Sejal Jadav
Unit-1
Principles of Object Oriented Programming Tokens,
expressions & Control Statements
(14 MARKS)
B.C.A & B.Sc.(IT) â 3
CS-13 C++ and Object Oriented
Programming
2. Prepared By: Asst. Prof. Sejal Jadav
Functions in c++
⢠What is Function?
⢠Definition, Declaration & Call
⢠Declaration
⢠Way to define a function
⢠Formal & Actual Arguments
⢠Types of formal arguments
⢠Call by value, call by address and call by reference
3. Prepared By: Asst. Prof. Sejal Jadav
What is Function?
⢠Function is block of code performing a unit
task.
⢠Function definition is block of code.
⢠Function is a way to achieve modularization.
(no. of small functions)
4. Prepared By: Asst. Prof. Sejal Jadav
⢠Function are of two types : Library (predefined)
function and user âdefined
⢠Predefined functions are
⢠declared in header files and
⢠defined in library files
5. Prepared By: Asst. Prof. Sejal Jadav
Function Definition, Declaration & Call
#include<iostream>
using namespace std;
main()
{
void fun();
Cout<<âYou are in mainâ;
fun();
}
void fun()
{
Cout<<âyou are in funâ;
}
Function Definition
Function Call
Function Declaration
Declaration of cout & cin
6. Prepared By: Asst. Prof. Sejal Jadav
Function Declaration [Function PROTOTYPE]
â˘Function Declaration is also known as Function
Prototype.
â˘Functions need to be declared before use (just
like variable).
7. Prepared By: Asst. Prof. Sejal Jadav
â˘Function can be declared locally or globally.
â˘Function has a name, return type and
arguments.
Syntax:
â˘ReturnType functionName(argumentList);
8. Prepared By: Asst. Prof. Sejal Jadav
Way to define a function
⢠No argument no return value
⢠With argument no return value
⢠No argument with return value
⢠With argument with return value
9. Prepared By: Asst. Prof. Sejal Jadav
5
6
Formal & Actual Arguments
#include<iostream>
int sum(int,int);
main()
{
int a=5,b=6;
int s=sum(a,b);
Cout<<âSum is :
â<<s;
}
int sum(int x, int y)
{
Return (x+y);
}
a
b
s
a & b are actual argument
Sum() memory
block
main()
memory
block
x & y are formal argument
5
6
11
x
y
10. Prepared By: Asst. Prof. Sejal Jadav
Types of Formal Argument
⢠Formal arguments can be of three types
⢠Ordinary variables of any type
⢠Pointer variables
⢠Reference variables
11. Prepared By: Asst. Prof. Sejal Jadav
⢠On the basis of arguments there are two types of
function are available in C++ language, they are;
17. Prepared By: Asst. Prof. Sejal Jadav
Call by reference
⢠When formal arguments are reference variables, it is
function call by reference.
⢠The call by reference method the called function
does not create its own copy rather it refers to
original value only by different name i.e. reference.
18. Prepared By: Asst. Prof. Sejal Jadav
⢠When we pass arguments by reference, the formal
arguments int sum(int &x,int &y) in the called
function becomes aliases to the actual arguments in
the calling function.
⢠(actual arguments -> aliases -> formal arguments )
⢠It means that when the called function is working
with its own arguments, it is actually working on the
original data.
19. Prepared By: Asst. Prof. Sejal Jadav
Inline function
Benefits of Function:
⢠Easy to read
⢠Easy to modify
⢠Avoids rewriting of same code
⢠Easy to debug
⢠Better memory utilization
20. Prepared By: Asst. Prof. Sejal Jadav
Function saves memory
â˘Function in a memory is to save memory space.
which becomes appreciable when a function is
likely to be called many times.
21. Prepared By: Asst. Prof. Sejal Jadav
Function is time consuming:
⢠However every time a function is called, it takes lot of
extra time in executing a series of instructions for
tasks such as jumping to the functions, saving
registers, pushing arguments into the stack and
returning to the calling function.
22. Prepared By: Asst. Prof. Sejal Jadav
⢠So, when function is small it is worthless to spend so
much extra time in such tasks in cost of saving
comparatively small space.
23. Prepared By: Asst. Prof. Sejal Jadav
Inline function
â˘To eliminate the cost of calls to small functions,
C++ proposes a new feature called inline
function.
â˘An inline function is a function that is expanded
in line when it is invoked.
24. Prepared By: Asst. Prof. Sejal Jadav
â˘Inline is a request not a command.
â˘The benefit of speed of inline functions reduces
as the function grows in size.
25. Prepared By: Asst. Prof. Sejal Jadav
â˘So the compiler may ignore the request in some
situations. Few of them:
⢠Function containing loops, switch, goto.
⢠Functions with recursion.
â˘Containing static variable.
26. Prepared By: Asst. Prof. Sejal Jadav
Syntax:
inline<function header>
{
Function body
}
29. Prepared By: Asst. Prof. Sejal Jadav
Default Arguments in C++ Functions
⢠The default arguments are used when you provide no
arguments or only few arguments while calling a
function.
⢠The default arguments are used during compilation of
program.
⢠For example, lets say you have a user-defined function
sum declared like this: int sum(int a=10, int b=20),
30. Prepared By: Asst. Prof. Sejal Jadav
⢠For example: int sum(int a=10, int b=20),
⢠now while calling this function you do not provide
any arguments, simply called sum(); then in this case
the result would be 30, compiler used the default
values 10 and 20 declared in function signature.
⢠If you pass only one argument like this: sum(80) then
the result would be 100, using the passed argument
80 as first value and 20 taken from the default
argument.
31. Prepared By: Asst. Prof. Sejal Jadav
⢠Letâs look at example:01_defaultArg.Cpp
32. Prepared By: Asst. Prof. Sejal Jadav
Rules of default arguments
⢠As you have seen in the above example that I have
assigned the default values for only two arguments b
and c during function declaration.
⢠It is up to you to assign default values to all
arguments or only selected arguments but remember
the following rule while assigning default values to
only some of the arguments:
33. Prepared By: Asst. Prof. Sejal Jadav
⢠If you assign default value to an argument, the
subsequent arguments must have default values
assigned to them, else you will get compilation error.
34. Prepared By: Asst. Prof. Sejal Jadav
⢠For example: Lets see some valid and invalid cases.
⢠Valid: Following function declarations are valid â
int sum(int a=10, int b=20, int c=30);
int sum(int a, int b=20, int c=30);
int sum(int a, int b, int c=30);
35. Prepared By: Asst. Prof. Sejal Jadav
⢠Invalid: Following function declarations are invalid â
⢠Since a(a variable) has default value assigned, all the
arguments after a (in this case b and c) must have
default values assigned
int sum(int a=10, int b, int c=30);
36. Prepared By: Asst. Prof. Sejal Jadav
⢠Since b has default value assigned, all the arguments
after b (in this case c) must have default values
assigned.
int sum(int a, int b=20, int c);
37. Prepared By: Asst. Prof. Sejal Jadav
⢠Since a has default value assigned, all the arguments
after a (in this case b and c) must have default values
assigned, b has default value but c doesn't have, thatâs
why this is also invalid
int sum(int a=10, int b=20, int c);
38. Prepared By: Asst. Prof. Sejal Jadav
Const arguments
⢠The constant variable can be declared using
const keyword.
⢠The const keyword makes variable value stable.
⢠The constant variable should be initialized
while declaring.
39. Prepared By: Asst. Prof. Sejal Jadav
⢠Syntax :
<function name> (const <type> <variable name>);
⢠The qualifier âconstâ tells the compiler that the
function should not modify the argument.
⢠Letâs Look at example: DefaultArg.cpp
40. Prepared By: Asst. Prof. Sejal Jadav
void add(const int a, int b)
{
a=a+b; //error
b=a+b;
cout<<âSum = â<<b;
}
main()
{
add(2,5);
}
⢠In the above program, the prototype
of function add declared two
arguments.
⢠Out of two arguments the first
argument is a constant. The second
argument is not constant.
⢠When function add is invoked, the
first argument a is a const according
to prototype of function.
⢠Any attempt made to change the
value of variable a will cause an
error message âcannot modify a
constant variable.â