Functions
Functions are used to calculate values. They are almost identical to procedures — they have a name, contain instructions, and can be called to perform their actions. The only difference is what happens at the end. When a function ends, it returns a value to the caller, whereas a procedure just ends. This means that procedures are called within statements, whereas functions can be called within expressions.
Functions — when, why, and how
Procedures are great as they let us group actions into isolated blocks of code. However, sometimes we need these blocks of code to calculate or get a value for us. In these cases we can create a function rather than a procedure. You can code the steps needed within the function to calculate or get the data needed, then anytime you need this value you can just call the function you created.
As with procedures, you can use a bottom-up or top-down approach to determining what functions your program will need. Again, you are likely to use both approaches in tandem.
An important difference between procedures and functions is how you write the instructions they contain. Because functions are conceptually different, they are used differently from procedures. Procedures just contain instructions, so they are used as [statements]. Functions, however, return a value, so are valid [expressions] by themselves and can be used within other expressions. Because of these different purposes, functions should generally not have any side effects when called (e.g., printing to the terminal). There are no syntactic rules enforcing this, it is just considered bad practice to do so.
In C/C++
The syntax for a function declaration is very similar to the syntax for a procedure declaration. There are only two differences:
- The function declaration starts with a data type name rather than
void
. - Most functions include a return statement in the block to specify what value will be returned.
Procedures as void functions
C/C++ does not have a strong distinction between functions and procedures. Technically, in C/C++ a procedure is just a function that returns a special void
type. However, although there isn’t really a difference in syntax between functions and procedures, it is still important to understand the conceptual difference between them.
Examples
The code below shows an example of a simple function.
It is called seconds_in_hour
, and returns an int
value.
In this example we are showing you the syntax of a function declaration in practice, but we are also highlighting the concept of scope.
Notice that both seconds_in_hour
and main
have variables called total
.
This is valid, and causes no conflicts, because each total
variable is limited to the scope of their respective function.
They are entirely separate variables, with different locations in memory.
This example function is pretty trivial, and probably isn’t something you would ever actually use — a constant would be much more appropriate given all the values involved are unchanging literals. Functions are much more useful when you can give them some data to work with. We will explore how to do that next.
Debugging
Now that we can declare functions and procedures, we also need to make sure we can debug them! This video will cover how to follow the flow of code by stepping in and out of functions, along with some common issues that might show up while stepping in C++.