C Study Guide

St. Gabriel's College

|

Functions

What is a Function

A Function is a piece of code that does one particular job. A Function is like a small program. We have used many Functions before:

You've probably seen functions in Math class, they're written like this:

If this function was in our program, the computer would think about it like this:

int number = f(4);
int number = 2*4 + 6;
int number = 14;

Parts of a Function

ReturnType function_name(parameters)
{
    return ReturnValue;
}

After the return statement, the function is done, and will do nothing more.

Examples of Functions

If a function has no Return Value, its Return type is void:

void this_does_nothing()
{
    printf("This function does nothing.\n");
}

Here are a couple of functions that have return values, but no parameters:

float this_returns_pi()
{
    return 3.14159268;
}

int this_returns_twelve()
{
    return 20 - 8;
}

Finally, this function has a return value and parameters:

int add(int n1, int n2)
{
    int sum = n1 + n2;
    return sum;
}

Local Variables and Scope

A Local Variable is a variable created inside of a function. A Local Variable can only be used inside the function where it is created. In this function, sum and avg are local variables:

float average(float n1, float n2, float n3)
{
    float sum = n1 + n2 + n3;
    float avg = sum / 3;
    return avg;
}

A function's Scope is the group of variables and functions that it knows about. A function only knows about:

For example: If you create a variable in main(), you cannot use it in average(), unless you give it as a Parameter. A function only knows about other functions if they are before it in the code.

Recursion

Sometimes, you can solve a problem by having a function call itself. This method of solving a problem is called Recursion. Let's look at how we can do that with Factorials:

Now there are two cases for any number n:

This means, we can write the function like this:

int factorial(int n)
{
    if(n > 1)
        return n * factorial(n-1);
    else
        return 1;
}

Recursion is also perfect for computing sequences like The Fibonacci Numbers:

int fibonacci(int n)
{
    if(n > 2)
        return fibonacci(n-2) + fibonacci(n-1);
    else
        return 1;
}