Introduction to Functions

Why use functions?

We will begin this chapter by learning to make simple modules (functions) that will help us solve a complex problem conveniently. Just like completing a puzzle using small jigsaw pieces, we will learn to solve problems using simple functions.

Do not be intimidated by the term functions. By the end of this chapter, we’ll have learned what functions exactly are and how and when to use them. So let us begin!

Say we wrote a solution to a small problem (for example, the sum-of-digits), and then, to solve a bigger problem, we needed that small problem’s solution again (the digit summation part) at multiple places. What would we do? Write the same solution again and again?

A bad approach

Yes! One idea could be to rewrite that same code again, wherever needed. This idea has issues through; here are some of the key issues:

  • It will be very repetitive.

  • Scaling applications with these code snippets will be very inconvenient to manage.

Think of a situation where we used a chunk of code written 50 times, and we later realized there was an error in that code. We would need to search and change all those 50 places wherever we had copied or written that code. This would be not only very inconvenient but also very inefficient! Our code would be lengthy, inefficient, and difficult to edit.

We need to come up with a smart solution.

The functional approach

This is where functions come in. We’ll see the need and use of functions throughout this lesson.

For now, think of a function as an abstract black box that takes some input and gives the desired output and can be used as a small module to solve a bigger problem. The module will take inputs (one or many, or none) called parameters. The function (the black box) will process these parameters and generate some required result/output.

The output of the module/function here does not mean printing on the console; here, output means the module will return a value that will replace wherever that function is called from.

Here is the pictorial representation of a function:

The working of functions

Observe the working of a function called add() in the animation below.

The add() function of return type int takes three int input parameters (x, y, and z).

In the animation above, when the second line of code in Task.cpp executes, the values of x, y, and z (called arguments) are passed into the function (the blue box where our function is implemented). In this module, the sum is calculated (10 + 20 + 30), and the result (that is 60) is returned and stored in the result variable.

Take your time understanding the animation above, especially how the function is called and the value gets returned and received for further processing.

What is the prototype of a function?

We need to keep three things in mind when writing a computer program.

  • Firstly, we need to think about how to divide our main problem into modules (functions).
  • Secondly, what particular task do we want each module (function) to perform on specific inputs.
  • Thirdly, we need to make sure we integrate everything and complete the main flow of the program. This stage is like matching the jigsaw puzzle (where every jigsaw piece will be a function call) by fixing the position of each piece (like calling an appropriate function at the right position).

We should make sure that when naming the functions, the function names are relevant and descriptive of what the function does.

Say we create a module that determines whether a game is over or not. Accordingly, we can name that module gameOver().

Identifying the module name along with its inputs and output types is called prototyping of the function. So, when writing a function prototype, we should know how many input parameters should be passed in our function along with their data types, and what should be the data type of the function itself. The output is called the return type of the function. The following is the syntax for defining a prototype in C++

return_type functionName(data_type parameter1, data_type parameter2, ...);

The prototype of the function can be considered as a function declaration. Writing the parameter names in the function prototype is not compulsory, but the data types of the parameters have to be defined.

Therefore, the prototype can be defined in the following way too:

return_type functionName(data_type, data_type, ...);

In the upcoming lessons, we’ll learn to design and implement functions.