Search⌘ K
AI Features

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 (AZ and az) and the underscore symbol (_). It may also contain digits (09), but the function name can’t start with those. For example, Key_2 is a valid function name, but 2_key is not. Function names are case-sensitive, meaning that name, Name, and NAME are 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.

C++
#include <iostream>
using namespace std;
void sayHello() // Header of the void function
{
cout << "Hello" << endl; // Printing hello
}
int main() // Main function
{
sayHello(); // Calling a function
return 0;
}

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:

C++
#include <iostream>
using namespace std;
void first()
{
cout << "Now inside the function 'first'." << endl;
}
void second()
{
cout << "Now inside the function 'second'." << endl;
}
int main()
{
cout << "Main program is starting here." << endl;
first(); // Calling first function
cout << "Back in main program." << endl;
second(); // Calling second program
cout << "Back in the main program again." << endl;
first(); // Calling first function again
cout << "Back in the main program after first again." << endl;
}

In the code above:

  • We define two simple functions—first() and second().
  • 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 cout statements.

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
C++
#include <iostream>
using namespace std;
int main()
{
// Write your code here
return 0;
}