Functions in C

Return to table of contents

Introduction

Functions in action


Functions represent logical blocks of code which are called upon by programs to perform some particular task.
In some cases, the program which calls the function may be required to send information to it and the function
may in turn return information when it has completed its duties.   The data passed to the function are known as
parameters.  In C (and many other languages) functions may only return at most one value.  They can however
return pointers to blocks of data in memory, or, manipulate values in a block of memory whose address had been
passed by the calling program.  If a function does return a value directly, its return type is 'void'.

Function declaration (prototyping) and implementation.

The following code demonstrates how to prototype and implement a simple function which will add the two
incoming numbers and return the result.
 

int MyAddition(int Value1, int Value2);

void main()
{
    int FirstValue;
    int SecondValue;
    int Result;
    FirstValue = 1;
    SecondValue = 2;
    Result = MyAddition(FirstValue, SecondValue);
}

int MyAddition(int Value1, int Value2)
{
    return ( Value1 + Value2 );
}

The first line of this small program is a prototype or forward declaration of the function MyAddition.  The
reason it is necessary is as follows:

When the compiler reaches the point in the code where the function MyAddition is called, it needs to know
if you (the programmer) have programmed it correctly i.e. have you passed the correct number and type of
parameters.  The only way it can know this is if you have told it previously that this particular function takes
two integers as arguments.  The function prototype does this.
Later on in the code, perhaps after the main function, you can type in the actual c-code which makes up your
function.  If your program makes use of many functions, it may be more convenient to use a header file which
includes all of the prototypes for these functions.  This header file can then be included at the top of any of your
program files, thus saving you the effort of entering and maintaining a long list of function prototypes in many
different files.  Go and take a look at the contents of say stdio.h in your c compiler's 'include' directory  (a word
of caution: make sure you don't change this file accidentally).   You will see many function prototypes as well as
perhaps some macro definitions.

Passing parameters.

There are two common ways in which paramters are passed.  The first is by value and the second is by reference.
When a parameter is passed by value to a function, the function receives a copy or clone of the original variable.  The
important thing to note about this is that no matter what the function does to this copy, it will not effect the original
variable in the calling program.

Passing by value

Consider the following small program.

#include <stdio.h>

void MyFunction(int InputParameter);

void main()
{
    int i;
    i = 0;
    printf("\nThe value of i is %d\n",i);
    MyFunction(i);
    printf("The new value of i is %d\n",i);

}
void MyFunction(int InputParameter)
{
    InputParameter++; /* only the copy of i is changed, the original is still as it was */
}

This code produces the following output on the screen:

The value of i is 0;
The value of i is 0;

and, as you can see the variable i remains unchanged.
 

Passing by reference

Suppose you now change the program above so that the address of i is passed instead as follows:

#include <stdio.h>

void MyFunction(int &InputParameter);

void main()
{
    int i;
    i = 0;
    printf("\nThe value of i is %d\n",i);
    MyFunction(&i);
    printf("The new value of i is %d\n",i);

}
void MyFunction(int &InputParameter)
{
    *InputParameter++;  /* Increment the integer at the given address */
}

This code produces the following output:
 

The value of i is 0;
The value of i is 1;

The key difference here is that in the second case, the function is passed the address of i.  When the
function changes the integer at this address (i) it changes the value of i in the calling program.  Passing
parameter addresses like this is called passing by reference.