Functions

Functions allow you to write a large chunk of code, and then reuse it over and over again by writing a single line. They can save a lot of time and space!

In C++, there are three basic steps to write a function:
 * 1) declare above main
 * 2) define below main
 * 3) call inside main (or another function)

Here are a few screencasts where I explain how to write a function in C++: https://watch.screencastify.com/v/43xR3Fda0iXgJZe9Zeu9

note: technically you can skip the declaration and just write the definition above main (like you do in python). However, I recommend getting in the habit of all three steps, as it will make it easier to move things into header files later.

Parameters are variables that are passed to a function when called. They appear in the parenthesis following the function's name. The return type of a function is what it hands back to whatever function called it. You can see the return type of a function before the function's name in both the declaration and definition of the function.

Functions with no parameters that return void
This is the simplest kind of function you can write. Notice the parenthesis are empty, which means there are no parameters, and the return type is void, which means it doesn't pass back anything to the function that called it:



Functions that have a non-void return type and parameters
Here are a few examples of C++ functions that return various types of variables and also have one or more parameters.