How Functions Work?

This lesson helps us understand how a function works i.e., how a function is called, how it performs the task and how it returns the result.

Calling a function #

Starting a function so that it achieves its task is called calling a function. The function call syntax is the following:

function_name(parameter_values)

The actual values that are passed to functions are called function arguments. Although the terms parameter and argument are sometimes used interchangeably in the literature, they signify different concepts.

The arguments are matched to the parameters one by one in the order that the parameters are defined. For example, the last call of printMenu() in the previous lesson uses the arguments Return and 1, which correspond to the parameters firstEntry and firstNumber, respectively.

Note: The type of each argument must match the type of the corresponding parameter.

Doing work #

In previous chapters, we have defined expressions as entities that do work. Function calls are expressions as well: they do some work. Doing work means producing value or having an effect on the program state:

  • Producing a value: Some operations only produce values. For ...