Functions

Introduction

Say you have the same code that runs in 40 different places in your program. If you wanted to make a change, would you like running through all those places and trying to change them all, or have this common functionality in one place, called from everywhere else when needed to? This is
the main purpose of functions.

Advantages

  • Re-usability
  • Convenience
  • Readability
  • Easier implementation

Usability

A functions consists of its name, what parameters it takes, and what it returns as result. In order to use the taken parameters in the function, they should be assigned a name, like a variable. Remember that C++ passes parameters by value1, not by reference2.

Syntax

return_type function_name(par_type1 par_name1, par_type2 par_name2, ... )
{
    // code goes here
 
    // All function except those that return void, should return something
    return whatever;
}

for example:

void printHello()
{
    std::cout << "Hello World!" << std::endl;
}

and:

int square(int value)
{
    return value*value;
}

I hope the above examples are self-explanatory.

Recursion

Just as you can call a function from within another function, you can also call a function from within itself. This is called recursion, and it makes a lot of algorithms much more easy to implement. But remember that it isn't always the best solution to a problem, because it's accepted that recursion is a bit slower than, for example, loop iteration.

Brief explanation

All computers have stacks, unto which they store program states. Among other things, they also store where in the execution stream the program is3. A function is stored elsewhere than the main body. When it's called, the OS pushes the current state & address on the stack, and literally jumps to the called functions' location in order to execute its commands. When the functions reaches its end, the stack pops the state, and the execution returns to whatever it was doing before calling that function. State also refers to whatever variables are declared in / are part of that function.

Functionality

Recursion can be used in many situation, for example fibonacci:

int fibonacci(int N)
{
    if (N <= 1)
        return 1;
 
    return fibonacci(N - 1) + fibonacci(N - 2);
}

or even, factorials:

int factorial(int N)
{
    if (N <= 1)
        return 1;
 
    return factorial(N - 1)*N; 
}

Just remember that in case you mess up the logic somewhere and the function ends up recursing for ever, it will crash your program because the stack will run out of space.

Conclusion

Functions are really useful in our day and age, you should use them as often as possible. Yes, performance may be important, but programmers who would try to extend or fix your code in the future are much more expensive than faster hardware. You need not worry about optimization anyway, the compiler does it for you now.

Happy coding ;-)

Next tutorial will be about Pointers and References

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 3.0 License