TLDR; functions are great cleaning up your code and create reusable code that you can use in many places
The full series
- Your first program
- Variables
- Working with input and output
- Functions, you are here
- Control flow
- Working with files
- Pointers
- Error management
- Structs and Classes
Resources
The "what" and "why" of functions
A function is a named reference containing a set of statements. The idea of a function is that you have set of statements that you see repeating in certain places in your code and you replace those with a function. Here's an example:
int random_value = rand % 10 + 1;
statement;
statement;
random_value = rand % 5 + 1;
statement;
statement;
random_value = rand % 20 + 1;
In the code above, you generate a random value with different interval, 1-10, 1-5, 1-20. It makes sense to break this out to a function, like so:
int getRandomValue(int range)
{
return rand % range + 1;
}
Then your code can be simplified like so:
int random_value = getRandomValue(10);
statement;
statement;
random_value = getRandomValue(5);
statement;
statement;
random_value = getRandomValue(20);
An added bonus is your code is also more readable, getRandomValue()
is easier to understand than rand() % 20 + 1
Functions
Ok now that we understand what functions are for, let's take it slow and explain what makes out a function. A function needs a name, parenthesis and a return type. Additionally, it needs to start and end with curly braces, like so:
<return type> <name>() {}
A more real example of the above would like so:
int getRandomValue()
{
/* body */
}
There are few concepts you need to know about functions like:
- header, this is the part of the function where you declare its name, return type and parameters, values you want to pass into the function. Here's the header from the above code:
int getRandomValue()
- body, the body of the function is where things happen, what you want the function to do. The body content is defined within the curly braces:
return rand % range + 1;
- definition = body + header, the header and body makes out what's called the function definition, like so:
int getRandomValue()
{
return rand % range + 1;
}
if you have sharp eyes, you might notice that range
isn't defined, it's because it's a parameter and we haven't defined it yet.
-
parameters, a parameter is a placeholder, a name and type capabable of storing what gets passed into a function. Here,
range
is the parameter:
int getRandomValue(int range)
{
return rand % range + 1;
}
-
invocation, to call or invocate a function, we need to call it by its name, and provide values to any parameters we might have defined. To call
getRandomValue
we need to add parenthesis and what's known as an argument (a value) to therange
parameter:
getRandomValue(10)
10 is the argument, the value, which is bound to the range
parameter. You can declare many parameters to a function like so for example:
int add(int x, int y)
{
return x + y;
}
and call it like so: add(2,2)
Return from a function
So far, you've seen how to declare function, add several parameters to it, but there's another important part of a function, what it returns. A function must have a return type, it's either void
or a data type like so:
void printSomething(string message)
{
cout << message;
}
if the the return type is something else than void
then you need a return
statement somewhere in the function body where you return the value, like so:
int add(int lhs, int rhs)
{
return lhs + rhs;
}
Without the return
keyword, you would get a compilation error.
Overloading, same functions defined many times with different params
When you defined a header, like so for example:
int add(int a, int b)
{
}
You have two parameters, what if you need three parameters or four, would I need to define a new function for each method? Fortunately, not, you can _overload_the function, meaning that you do define more functions, but they will all have the same name with the difference being that they have different number of parameters, like so:
int add(int a, int b)
{
return add(a,b,0);
}
int add(int a, int b, int c)
{
return add(a,b,c, 0);
}
int add(int a, int b, int c, int d)
{
return a + b + c + d;
}
all this code enables us to use the version of add()
we want, when we need it. Here's an example of using all four variants:
int sum = add(2,3); // 5
sum = add(2,3,4); // 9
add(2,3,4,5) // 4