Simple User-defined Functions
Explore how to define and call simple user-defined functions in C++. Understand function components, naming rules, and practice writing reusable code modules to improve program structure and efficiency.
Function
We can divide a program into procedural components or modules called functions in C++. We are already familiar with a function, main(). Functions can be reused in a programming technique called the modular or procedural approach, also known as the divide and conquer approach. We should always try to design functions that can be copied and reused in other programs too.
Broadly, there are two types of functions, which we’ll explore in the following sections.
Structure of a function
The following slides illustrate various parts of a function definition:
A function name can only contain letters (
A—Zanda—z) and the underscore symbol (_). It may also contain digits (0–9), but the function name can’t start with those. For example,Key_2is a valid function name, but2_keyis not. Function names are case-sensitive, meaning thatname,Name, andNAMEare three different functions.
There are two main parts of a function definition:
- Header: The first line, ending with closing parenthesis
). - Body: The block of statements below the function header starting with an opening brace
{and ending with a closing brace}.
The function header contains the keyword void (it means that the function does not have a return value), the function name sayHello, and the parameters in (). All the statements in the function body are written as a block of code enclosed in { and }. We cannot write the body of the function without braces.
Function call
We can call a function by writing the function name followed by () ending with ;.
The following program demonstrates the creation of a function and a function call.
Line 11: We instruct the program to call the sayHello() function. This is a function call.
Example of function calls
Let’s look at the following code:
In the code above:
- We define two simple functions—
first()andsecond(). - We call the
first()function twice to demonstrate that a function can be reused as many times as needed. - We demonstrate the sequence of execution with the help of
coutstatements.
Practice creating and calling functions
The following are a few example programs to practice creating and calling functions. By clicking the “Show Solution” button, you’ll find a program that solves the respective problem. You may copy and paste the given solution into the code widget to make sure that the output of your solution matches the given solution. There may be several ways of writing correct solutions in programming.
Function to create a multiplication table
Write a function showTableOf4() that displays 20 terms of the table of 4. Complete your program by writing the main function to call the showTableOf4() function.
Sample output
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
4 x 11 = 44
4 x 12 = 48
4 x 13 = 52
4 x 14 = 56
4 x 15 = 60
4 x 16 = 64
4 x 17 = 68
4 x 18 = 72
4 x 19 = 76
4 x 20 = 80