9. Functions
0. Getting Started
1. Optional Introduction to C++ and Programming Concepts
2. So… How Do I Use and Code in C++
3. Inputs, Outputs and Comments
4. Initialization of Variables
5. Other Data Types
6. Number Operators
7. Control Flow
8. Vectors
9. Functions
Project 1 - Statistics Program
Project 2 - Rocket Simulation
Project 3 - Airline Management
Much of what we’ve shown you so far has been introducing the building blocks of coding in C++; you’ve seen several different data types, common iteration methods (the multiple kinds of loops), and a versatile container, the vector type. With this knowledge, you can now do many things in C++. But what kinds of things can you write? And how are they useful?
One of the most powerful tools of coding is that we can create functions to perform certain calculations or actions for us at will. We can call functions whenever we want after we define the function itself, which allows us a great deal of flexibility in using it. We have to define functions just like we do variables, so the C++ compiler knows what we want something with our function name to do. Below is an example of a function with the majority of the body omitted so you can see the different parts of a function definition and how the return works.
This is the outline of a function, with it’s five necessary components marked in the red bubbles - below are explanations for each component:
This is the return type for the function. All functions need to have a return type that signifies what the output of the function is. In the snippet above, this function has a return type of a double, so its output must be a double. With functions, the void type becomes particularly useful and common as many functions do not have any output and thus return void.
This is the function name, which is what you’ll use to call the function in your program (we’ll see this later). In this case, the function name is “stdev”, which hints that it’s a function to calculate the standard deviation of a set of values. The code that actually does this is omitted here for simplicity, but the code of the function from which this was adapted does just that. Function names should be short but as descriptive as possible. You don’t want to be taking up your entire line with the function name each time you call it but you also want to make sure you can remember what it does from its name.
This is the input parameter type which lets us know what kind of input the function needs. In this case, the function requires a vector of doubles to operate. Functions can also have no inputs and run without needing any additional outside information or they can have multiple inputs, separated by commas.
This is the input parameter which allows us to use the object in the function code to do whatever we need to do. It’s not shown in the code snippet above, but we would call this function with a pre-existing vector of doubles which the function itself can then use to calculate the standard deviation. This input called “v” in this case could be used as if we declared it within the function since input parameters can be used directly in the function.
This is the return statement which is necessary for all functions, even those that return void (the line just says “return;” in those cases). This tells the compiler the output of the function. The variable after the keyword “return” must have the same type as the return type of the function, or else the program will likely throw an error and not work. The value in the return statement can be an input parameter if you want, or it can be a variable defined in the function code itself (as shown in the snippet above).
Now that we’ve looked at how to define a function, let’s look at how to call it later in our program. For this example, we assume that the stdev function has been completed, i.e., the comment in the previous code snippet was replaced with all the code needed to make the function work. The function itself works using concepts beyond the scope of this introduction to C++, so we’ll move forward assuming that it works as intended.
In this code snippet, we have the main program that would run after (and only after) the definition of the stdev function, otherwise the program crashes and tells us in the error message that “stdev is not declared in this context”. In it, we create a vector of doubles called “vExample” and then call the stdev function using it. We define a variable to hold the value of the function’s output which must be a double since that is the function’s return type, and call the function using the ( ) operand. The function only needs one input that must be a vector of doubles, so we pass vExample as a parameter. The program will then execute the code contained within the function and then return the final value to main. The last thing we do is cout the value from the stdev function, which outputs to the console the calculated standard deviation: