Chapter 6 - Functions
Chapter 6 - Functions
6.1. Overview
A function is a collection of statements that performs a specific task. So far you have used
functions in two ways: (1) you have created a function called main() in every program you have
written, and (2) you have called library functions such as sqrt(). In this chapter you will learn
how to create your own functions that can be used like library functions.
Note: All C++ programs must contain the function main( ). The execution of the program starts
from the function main( ).
Functions are building blocks of the programs. They make the programs more modular and easy
to read and manage. One reason to use functions is that they break a program up into small,
manageable units. Each unit is a module, programmed as a separate function. Imagine a book
that has a thousand pages, but isn’t divided into chapters or sections. Trying to find a single topic
in the book would be very difficult. Real-world programs can easily have thousands of lines of
code, and unless they are modularized, they can be very difficult to modify and maintain.
Another reason to use functions is that they simplify programs. If a specific task is performed in
several places in a program, a function can be written just once to perform that task, and then be
executed anytime it is needed.
A C++ program can contain any number of functions according to the needs. The general form
of the function is: -
The function of consists of two parts function header and function body. The function header is:-
The return_type specifies the type of the data the function returns. The return_type can be void
which means function does not return any data type. The function_name is the name of the
function. The name of the function should begin with the alphabet or underscore. The parameter
list consists of variables separated with comma along with their data types. The parameter list
could be empty which means the function do not contain any parameters. The parameter list
Page 1 of 8
Programming I 2023
A function definition contains the statements that make up the function. When creating a
function, you must write its definition. All function definitions have the following parts:
Name: Every function must have a name. In general, the same rules that apply to variable names
also apply to function names.
Parameter list: The program module that calls a function can send data to it. The parameter list
is the list of variables that hold the values being passed to the function.
Body: The body of a function is the set of statements that carry out the task the function is
performing. These statements are enclosed in a set of braces.
Return type: A function can send a value back to the program module that called it. The return
type is the data type of the value being sent back.
Page 2 of 8
Programming I 2023
#include <iostream.h>
//*****************************************
// Definition of function displayMessage *
// This function displays a greeting. *
//*****************************************
void displayMessage()
{ Program Output
cout << "Hello from the function displayMessage.\n";
}
int main() Hello from main.
{ Hello from the function displayMessage.
cout << "Hello from main.\n"; Back in function main again.
displayMessage(); // Call displayMessage
cout << "Back in function main again.\n";
return 0;
}
The function displayMessage is called by the following line in main:
displayMessage();
This line is the function call. It is simply the name of the function followed by a set of
parentheses and a semicolon. Let’s compare this with the function header:
Function Header……void displayMessage()
Function Call ………displayMessage();
Page 3 of 8
Programming I 2023
A function prototype eliminates the need to place a function definition before all calls to the
function. Before the compiler encounters a call to a particular function, it must already know
certain things about the function. In particular, it must know the number of parameters the
function uses, the type of each parameter, and the return type of the function. Parameters allow
information to be sent to a function. Certain return types allow information to be returned from a
function.
One way of ensuring that the compiler has this required information is to place the function
definition before all calls to that function.
Another method is to declare the function with a function prototype. Here is a prototype for the
displayMessage function in the above example program:
void displayMessage();
This prototype looks similar to the function header, except there is a semicolon at the end. The
statement tells the compiler that the function displayMessage has a void return type, meaning it
doesn’t return a value, and it uses no parameters.
Note: Function prototypes are also known as function declarations.
When a function is called, the program may send values into the function. Values that are sent
into a function are called arguments. A parameter is a special variable that holds a value being
passed as an argument into a function. By using parameters, you can design your own functions
that accept data this way. Here is the definition of a function that uses a parameter:
void displayValue(int num)
{
cout << "The value is " << num << endl;
}
Notice the integer variable definition inside the parentheses (int num). The variable num is a
parameter. This enables the function displayValue to accept an integer value as an argument.
Example 2
You’ve seen that information can be passed into a function by way of its parameters. Information
can also be returned from a function, back to the part of the program that called it. Although
several arguments can be passed into a function, only one value can be returned from it.
The data type of the return value precedes the function name in the function header and
prototype. The following prototype declares a function named square that accepts an integer
argument and returns an integer:
int square(int);
Here is the definition of the function:
int square(int number)
{
return number * number;
}
Page 5 of 8
Programming I 2023
A local variable is defined inside a function and is not accessible outside the function. A global
variable is defined outside all functions and is accessible to all functions in its scope.
Just as you’ve defined variables inside function main, you may also define them inside other
functions. Variables defined inside a function are local to that function. They are hidden from the
statements in other functions, which normally cannot access them. The example program below
shows that because the variables defined in a function are hidden, other functions may have
separate, distinct variables with the same name.
Although local variables are safely hidden from other functions, they do not provide a
convenient way of sharing data. When large amounts of data must be accessible to all the
functions in a program, global variables are an easy alternative.
A global variable is any variable defined outside all the functions in a program. The scope of a
global variable is the portion of the program from the variable definition to the end. The
following program shows two functions, main and anotherFunction, which access the same
globalvariable, num.
Program Output
int main()
{
cout << "In main, num is " << num << endl; In main, num is 2
In anotherFunction, num is 2
Pageto750
But, it is now changed of 8
Back in main, num is 50
Programming I 2023
Note: Remember that local variables are not automatically initialized like global variables are.
The programmer must handle this.
Page 8 of 8