noobtuts

C++ Tutorial for noobs - Part 5: Functions

...Back to Part 4: Variables

Motivation

Let's say we want have an algorithm that increase a variable by one, and then multiplies it with two, then divides it by three, then subtracts four from it.

We want to do try this algorithms for the numbers 42, 43, 44, 45, 46 and 47.

Since we already know C++ Variables, we could do it pretty easily like this:

int a = 42;
a = a + 1;
a = a * 2;
a = a / 3;
a = a - 4;

int b = 43;
b = b + 1;
b = b * 2;
b = b / 3;
b = b - 4;

int c = 44;
c = c + 1;
c = c * 2;
c = c / 3;
c = c - 4;

int d = 45;
d = d + 1;
d = d * 2;
d = d / 3;
d = d - 4;

int e = 46;
e = e + 1;
e = e * 2;
e = e / 3;
e = e - 4;

int f = 47;
f = f + 1;
f = f * 2;
f = f / 3;
f = f - 4;

Reminder: to see what value a variable has, we can use std::cout<<a; any time.

Now that's a lot of work for doing the same thing over and over again...

We could make our life much easier if we would put the algorithm into a function:

int my_algorithm(int n)
{
    n = n + 1;
    n = n * 2;
    n = n / 3;
    n = n - 4;
    return n;
}

a = my_algorithm(42);
b = my_algorithm(43);
c = my_algorithm(44);
d = my_algorithm(45);
e = my_algorithm(46);
f = my_algorithm(47);

Here is what happens:

Holy Moly!

Theory

Each C++ function hs the same structure:

.
 ------------------------ function type (like int)
 |
 |      ----------------- function name (like Abc_123)
 |      |
 |      |        -------- the parameters (many or none)
 |      |        |
int increase(int n)
{                   -|
    return n + 1;    |--- function body ( {...} )
}                   -|

There are a few things to know about functions:

Examples

Here are a few examples of all kinds of different functions:

// a function that adds two numbers
int add(int n, int m)
{
    return n + m;
}

// usage:
int a = 1;
int b = 2;
int c = add(a, b); // c is 3
int x = add(1, 2); // x is 3

 
// a function that just returns 24
int twentyfour()
{
    return 1 * 2 * 3 * 4;
}

// usage:
int a = twentyfour(); // a is 24
twentyfour(); // calculates 24 but doesn't store it

 
// a function that simply increases a number
int increase(int n)
{
    return n + 1;
}

// usage:
a = increase(0); // a is 1
b = increase(increase(0)); // b is 2

About Scope

Let's take a look at our increase function again:

int increase(int n)
{
    return n + 1;
}

There is one more thing to know about this: the variable n only exists within the function, or in other words, we can only use n between { and }.

So this does not work:

int a = increase(0);
int b = a + n; // n is only known inside of increase

Call-by-reference & Call-by-value

Foreword

Please don't get scared by the explanation of the two concepts, in the end they are kinda optional, but it's good to have read about them once.

Generally speaking, there are two types of functions:

Call-by-Value

For example, our increase function above takes something and then returns whatever it took, increased by one:

int increase(int n)
{
    return n + 1;
}

The important part is how we use it:

int a = 0;
int b = increase(a);

In the above example, a is passed as a parameter into the increase function, but it is never modified. In the end, a is 0 and b is 1.

What happens behind the scenes is this:

Here is the way that our a travels:
Call-by-value Flow
As we can see, a never goes inside of increase, instead it's copied into n.

Call-by-value is just that. It means that when calling a function, the parameters become copies of whatever we passed into it.

Call-by-Reference

The counter-part is this:

void increase(int &n)
{
    n = n + 1;
}

Again, the important part is how we use it:

int a = 0;
increase(a);

In the above example, a is passed as a parameter into the increase function directly, without a being copied. We did this by naming our variable &n instead of n. & means it's not copied.

Here is what happens behind the scenes:

Here is the way that our a travels:
Call-by-reference Flow
As we can see, a goes all the way through increase, and even though we named it n for a while, we still work with a. All this happens simply because we put a & sign in front of our parameter.

Summary

The whole call-by-value and call-by-reference thing can be confusing.
Here is a final overview:

If you love simplicity then stick with call-by-value until you fully understand C++.

Good news: now that we know Data-Types, Variables and Functions, we have a solid knowledge of the C++ fundamentals and we can already write some simple programs!

Continue to Part 6: If, Else and Switch...