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.
We'll cover the following
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 example, a function that adds numbers would be producing the result of that addition. As another example, a function that makes a
Student
object by using the student’s name and address would be creating aStudent
object. -
Having side effects: Effects are any changes in the state of the program or its environment. Some operations have only side effects. An example is how the
printMenu()
function above changesstdout
by printing to it. As another example, a function that adds aStudent
object to a student container would also have a side effect: it would be causing the container to grow.
In summary, operations that cause a change in the state of the program have side effects. -
Having side effects and producing a value: Some operations do both. For example, a function that reads two values from
stdin
and returns their sum would have side effects due to changing the state ofstdin
and also produce the sum of the two values. -
No operation: Although every function is designed as one of the three categories above, depending on certain conditions at the compile time or at the run time, some functions end up doing no work at all.
The return value #
The value that a function produces as a result of its work is called its return value. This term comes from the observation that once the program execution branches into a function, it eventually returns to the point from where the function was called. Functions get called, and they return values.
Just like any other value, return values have types. The type of the return value is specified right before the name of the function, at the point where the function is defined. For example, a function that adds two values of type int
and returns their sum also as an int
would be defined as follows:
int add(int first, int second) {
// ... the actual work of the function ...
}
The value that a function returns takes the place of the function call itself. For example, assuming that the function call add(5, 7)
produces the value 12, then the following two lines would be equivalent:
writeln("Result: ", add(5, 7));
writeln("Result: ", 12);
In the first line above, the add()
function is called with the arguments 5
and 7
before writeln()
gets called. The value 12 that the function returns is in turn passed to writeln()
as its second argument.
This allows passing the return values of functions to other functions in complex expressions:
writeln("Result: ", add(5, divide(100, studentCount())));
In the line above, the return value of studentCount()
is passed to divide()
as its second argument. Then, the return value of divide()
is passed to add()
as its second argument, and eventually,a the return value of add()
is passed to writeln()
as its second argument.
The return
statement #
The return value of a function is specified by the return
keyword in the body of the function:
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy