Function
CHAPTER 5
Why use Function
You can simplify programming tasks by breaking
programs into smaller logical components.
Function are used to divide a large code into module, due
to this we can easily debug and maintain the code.
E.g. if we write a calculator programs at that time we can
write every logic in a separate function [For addition
sum(), for subtraction sub()].
Any function can be called many times.
Code Re-usability
Develop an application in module format.
Easily to debug the program.
Code optimization: No need to write lot of code.
Computer Programming 03/27/2025 2
Function Definitions
Function definition: statements that make up a
function
A function is a group of statements that together
perform a specific task.
Every C++ program has at least one function, which is
main().
Every function that is called in a program must be
defined somewhere (once only).
return_type
The syntax is: function_name(parameter)
{
function body;
Computer Programming
} 03/27/2025 3
Function Definition
Definition includes:
return type: data type of the value that function
returns to the part of the program that called it
name: name of the function. Function names follow
same rules as variables
parameter list: variables containing values passed to
the function
body: statements that perform the function’s task,
enclosed in {}
Computer Programming 03/27/2025 4
Function Definition
Note: The line that reads int main()is the
function header.
Computer Programming 03/27/2025 5
Function Return Type
If a function returns a value, the type of the
value must be indicated:
int main()
If a function does not return a value, its
return type is void:
void printHeading()
{
cout << "Monthly Sales\n";
}
Computer Programming 03/27/2025 6
Function Declarations
A function declaration is the process of tells the
compiler about a function name.
Then, the actual body of the function can be defined
separately.
The syntax is: return_type
function_name(parameter);
Computer Programming 03/27/2025 7
Function Calling
The syntax is: function_name(); // or
variable=function_name(argument);
Computer Programming 03/27/2025 8
Example: sum() function Print out:
#include<iostream.h> Sum:
33
void sum(); // declaring a function
int a = 11, b = 22, c;
int main()
{
sum(); // calling the function
return 0;
}
void sum() // defining the function
{
c = a + b;
cout << "Sum: " << c;
Computer Programming 03/27/2025 9
}
example
Computer Programming 03/27/2025 10
Type of Function
There are two type of function in C++ Language. They
are:
Library function or pre-define function.
User defined function.
Computer Programming 03/27/2025 11
Library Function
Library functions are the built-in function in C++
programming.
Programmer can use library function by invoking function
directly;
#include they don't need to write it themselves, for example:
<iostream>
#include <cmath> // includes the content of cmath file.
using namespace std;
int main() Print out:
{ Enter a number: 26
double number, squareRoot; Square root of 26 =
cout << "Enter a number: "; 5.09902
cin >> number;
// sqrt() is a library function to calculate square root.
squareRoot = sqrt(number);
cout << "Square root of " << number << " = " << squareRoot;
return 0;
}
Computer Programming 03/27/2025 12
Sending Data into a
Function
Can pass values into a function at time
of call:
c = pow(a, b);
Values passed to function are arguments
Variables in a function that hold the
values passed as arguments are
parameters
Computer Programming 03/27/2025 13
A Function with a Parameter Variable
void displayValue(int num)
{
cout << "The value is " << num << endl;
}
The integer variable num is a parameter.
It accepts any integer value
passed to the function.
Computer Programming 03/27/2025 14
Passing value or data to function
There are two ways to pass value or data to function.
call by value
call by reference
Call by value: In call by value, original value can not be
changed or modified.
Call by reference: In call by reference, original value is
changed or modified because we pass a reference (an
address). So actual and formal arguments shares the same
address space. Hence, any value changed inside the function, is
reflected inside as well as outside the function.
Computer Programming 03/27/2025 15
Continued
The value of num1 and num2 are initialized to
variables a and b respectively. These arguments a and b are
called formal arguments. For example:
Computer Programming 03/27/2025 16
Return Statement
A function can return a single value to the calling
program using return statement. In the previous
program, the value of add is returned from user-defined
function to the calling program using statement below:
Computer Programming 03/27/2025 17
Returning a Value From a Function
A function can return a value back to the
statement that called the function.
You've already seen the pow function, which
returns a value:
double x;
x = pow(2.0, 10.0);
Computer Programming 03/27/2025 18
Returning a Value From a Function
In a value-returning function, the return
statement can be used to return a value
from function to the point of call. Example:
int sum(int num1, int num2)
{
double result;
result = num1 + num2;
return result;
}
Computer Programming 03/27/2025 19
A Value-Returning Function
Return Type
int sum(int num1, int num2)
{
double result;
result = num1 + num2;
return result;
}
Computer Programming 03/27/2025 20
A Value-Returning
Function
int sum(int num1, int num2)
{
return num1 + num2;
}
Functions can return the values of
expressions, such as num1 + num2
Computer Programming 03/27/2025 21
Example: Call by Value function
#include<iostream.h>
void swap(int a, int b){
int temp;
temp = a; Print out:
a = b; Value of a: 111
b = temp; Value of b: 222
}
void main(){
int a=111, b=222;
swap(a, b); // passing value to function
cout<<"Value of a: "<< a <<endl;
cout<<"Value of b: "<< b <<endl;
}
Computer Programming 03/27/2025 22
Example: Call by Reference
function
#include<iostream.h>
void swap(int *a, int *b){
int temp;
temp = *a; Print out:
*a = *b; Value of a: 222
*b = temp; Value of b: 111
}
void main(){
int a=111, b=222;
swap(&a, &b); // passing value to function
cout<<"Value of a: "<< a <<endl;
cout<<"Value of b: "<< b <<endl;
}
Computer Programming 03/27/2025 23
Summary: Argument
Passing
Arguments passed by value and by reference.
For example:
When f() is called, val++ For example: int
increments a local copy of main(){
the first actual argument,
whereas ref++ int i=1;
increments the second
actual argument. int j=1;
Computer Programming 03/27/2025 24
Continued
Difference Between Call by Value and Call by Reference.
call by Value call by Reference
This method copy address of
This method copy original value
arguments into function as a
into function as a arguments.
arguments.
Changes made to the parameter
Changes made to the parameter
affect the argument. Because
inside the function have no
address is used to access the
effect on the argument.
actual argument.
Actual and formal arguments Actual and formal arguments
will be created in different will be created in same memory
memory location location
Note: By default, C++ uses call by value to pass arguments.
Computer Programming 03/27/2025 25
Function Overloading
Two or more functions having same name but different
argument(s) are known as overloaded functions.
In Function Overloading “Function” name should be the same
and the arguments should be different.
Here, all 4 functions are overloaded functions called “test”
because argument(s) passed to these functions are different.
For example: int test() { }
int test(int a) { }
float test(double a) { }
int test(int a, double b) { }
N.B., the return type of all these 4 functions are not same.
Overloaded functions may or may not have different return
type but it should have different argument(s).
double test(int a) { }
int test(int b) { }
Computer Programming
// ERROR CODE!!!03/27/2025 26