04 CP_lecture-04_C++ Functions in C++
04 CP_lecture-04_C++ Functions in C++
Using
April-2025
Chapter IV
Functions in C++
2
Objectives
To understand how to construct programs modularly from
small pieces called functions.
3
Introduction
A function is a group of statements that together perform a
task. Every C++ program has at least one function, which is
main(), and all the most trivial programs can define additional
functions.
6
I. Library Functions
Usually called system defined functions. These are available to
anyone who writes a C++ program.
7
Cont…
For example this are some of the predefined mathematical
functions pow(x, y), sqrt(x), and floor(x).
9
Cont….
The following table shows some of the commonly used built in
functions
10
Cont…
Example: library function
11
Cont…
Output of the above code is
Enter a number: 9
Square root of 9 = 3
13
Cont…
14
Cont…
15
II. User Defined Functions:
Because of C++ does not provide every function that you will
ever need and designers cannot possibly know a user’s specific
needs, we must learn how to write our own functions. In this
chapter we discuss in detail about user defined functions.
17
Cont…
A C++ function definition consists of a function header and a
function body. Here are all the parts of a function:
Return Type: A function may return a value. The return_type is
the data type of the value the function returns. Some functions
perform the desired operations without returning a value. In this
case, the return_type is the keyword void.
Function Name: This is the actual name of the function. The
function name and the parameter list together constitute the
function signature.
Parameters: A parameter is like a placeholder. When a function is
invoked (called), you pass a value to the parameter. This value is
referred to as actual parameter. The parameter list refers to the
type, order, and number of parameters of a function. Parameters
are optional; that is, a function may contain no parameters.
Function Body: The function body contains a collection of
statements that define what the function does. 18
Cont…
2.2 How user-defined-function works in C++ P_Language
Consider the figure below. When a program begins running, the
system calls the main() function, that is, the system starts
executing codes from main() function.
When control of program reaches to function_name() inside
main(), the control of program moves to void function_name(),
all codes inside void function_name() is executed and control of
program moves to code right after function_name() inside
main() as shown in figure bellow.
19
Cont…
Example: C++ program to add two integers. Make a function
add() to add integers and display sum in main() function.
20
Cont..
2.3 Function Prototype(declaration)
A function declaration tells the compiler about a function name
and how to call the function. The actual body of the function
can be defined separately.
21
Cont…
In C++, function prototype is a declaration of function without
function body to give compiler information about user-defined
function. Function prototype in above example:
23
Cont…
To execute the codes of function body, the user-defined
function needs to be invoked (called). In the above program,
add(num1,num2); inside main() function calls the user-defined
function. In the above program, user-defined function returns
an integer which is stored in variable add.
24
Cont…
Let’s look at the following example:
25
Cont…
26
Cont…
2.5 Passing Arguments to Function
27
Cont…
In above example, two variables, num1 and num2 are passed
to function during function call.
These arguments are known as actual arguments. The value of
num1 and num2 are initialized to variables a and b
respectively.
These arguments a and b are called formal arguments. This is
demonstrated below:
28
Cont…
Notes on passing arguments
The numbers of actual arguments and formals argument should be
same. (Exception: Function Overloading)
The type of first actual argument should match the type of first
formal argument. Similarly, type of second actual argument should
match the type of second formal argument and so on.
In above program, both arguments are of int type. But it's not
necessary to have both arguments of same type. 29
Cont…
Return Statement
30
Cont…
The figure below demonstrates the working of return statement:
31
Cont…
In the above program, the value of add inside user-defined
function is returned to the calling function.
34
Cont…
Which shows that there is no change in the values though they had been
changed inside the function. 35
Cont…
Call by reference: The call by reference method of passing
arguments to a function copies the reference of an argument
into the formal parameter. Inside the function, the reference is
used to access the actual argument used in the call. This means
that changes made to the parameter affect the passed
argument.
37
Cont…
38
Cont….
2.7 C++ User-defined Function Types
For better understanding of arguments and return in functions,
user-defined functions can be categorised as:
40
Cont…
41
Cont…
2. Function with no arguments but return value:
42
Cont…
43
Cont…
3. Function with argument but no return value:
44
Cont…
45
Cont…
4. Function with arguments and return value.
46
Cont…
NOTE: All four programs above gives the same output and all
are technically correct program. There is no hard and fast rule
on which method should be chosen. The particular method is
chosen depending upon the situation and how a programmer
want to solve that problem.
47
Function Overloading
In C++ programming, two functions can have same
identifier(name) if either number of arguments or type of
arguments passed to functions are different. These types of
functions having similar name are called overloaded functions.
Example of function overloading
48
Cont…
All 4 functions mentioned above are overloaded function. It
should be noticed that, the return type of all 4 functions is
same,i.e, int. Overloaded function may or may not have
different return type but it should have different
argument(either type of argument or numbers of argument
passed). Two functions shown below are not overloaded
functions because they only have same number of arguments
and arguments in both functions are of type int.
Both functions has same number of argument and same type of
argument
Hence, functions mentioned below are not overloaded functions.
49
Compiler shows error in this case.
Cont…
50
Cont…
51
Cont…
52
Cont…
Example 2: Function Overloading: C++ Program to return
absolute value of variable types integer and float using function
overloading
53
Cont…
54
Cont…
55
Default Argument
In C++ programming, you can provide default values for
function parameters. The idea behind default argument is very
simple. If a function is called by passing argument/s, those
arguments are used by the function. But if all argument/s are
not passed while invoking a function then, the default value
passed to arguments are used. Default value/s are passed to
argument/s in function prototype. Working of default argument
is demonstrated in the figure below:
56
Cont…
57
Cont…
58
Cont….
• Example: Default Argument: C++ Program to demonstrate
working of default argument
59
Cont…
In the above program, you can see default value passed to arguments(in function
prototype). At first, display() function is called without passing any arguments. In
this case, default() function used both default arguments. Then, the function is
called using only first argument. In this case, function does not use first default
value passed. Function uses the actual parameter passed as first argument and
takes default value(second value in function prototype) as it's second argument.
When display() is invoked passing both arguments, default arguments are not
used.
Note: The missing argument must be the last argument of the list, that is, if
you are passing only one argument in the above function, it should be the
first argument. 60
Function Recursion
In many programming languages including C++, it is possible to call a
function from a same function. This function is known as recursive function
and this programming technique is known as recursion.
To understand recursion, you should have knowledge of two important
aspects:
In recursion, a function calls itself but you shouldn't assume these two functions
are same function. They are different functions although they have same name.
Local variables are variables defined inside a function and has scope only inside
that function. In recursion, a function call itself but these two functions are
different functions (You can imagine these functions are function1 and function
2. The local variables inside function1 and function2 are also different and can
only be accessed within that function.
The C++ programming language supports recursion, i.e., a function to call
itself. But while using recursion, programmers need to be careful to define
an exit condition from the function, otherwise it will go into an infinite loop.
61
Cont…
• Example 1: Consider this example to find factorial of a number
using recursion.
62
Cont…
Explanation: How recursion works
63
Cont…
Suppose user enters 4 which is passed to function factorial().
Here are the steps involved:
In first factorial() function, test expression inside if statement is
true. The statement return num*factorial(num-1); is executed,
which calls second factorial() function and argument passed is
num-1 which is 3.
In second factorial() function, test expression inside if statement is
true. The statement return num*factorial(num-1); is executed,
which calls third factorial() function and argument passed is num-1
which is 2.
In third factorial() function, test expression inside if statement is
true. The statement return num*factorial(num-1); is executed,
which calls fourth factorial() function and argument passed is num-
1 which is 1.
64
Cont…
The fourth factorial() function, test expression inside if statement is
false. The statement return 1; is executed, which returns 1 to third
factorial() function.
65
Cont…
Example 2: The following example generates the Fibonacci
series for a given number using a recursive function:
66
67