learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!
VB - soon here!

Search

 

 

 

 

Next Back

 

Lesson 12: Functions

In this lesson you will learn:

  • how to use functions
  • how to declare your own functions
  • what prototypes are
  • declaring functions

Q: What happened to the blonde tap dancer?
A: She slipped off and fell down the drain.

This part should be quite simple compared to the last part. Functions are easy to use; they allow complicated programs to be broken up into small blocks, each of which is easier to write, read, and maintain. We have already encountered the function main and made use of printf from the standard library. Now let's look at writing and using our own functions.

Functions have a basic structure. Their format is:

return_data_type function_name (arguments, arguments)
data_type_declarations of arguments;
{

    function_body

}

A function can be thought of as a mini-program, where a group of statements are executed when the function is called. A function is CALLED (or INVOKED) when you need to branch off from the program flow and execute the group of statements within that function. Once the statements in the function are executed, program flow resumes from the place where you called your function.

Functions have 5 main features:

1.
The RETURN TYPE is the data type of the RETURN VALUE of the function.


2.
The NAME is required as an identifier to the function, so that the computer knows which function is called. Naming of functions follows the same set of rules as the naming of variables.


3.
Functions can take ARGUMENTS - a function might need extra information for it to work. Arguments are optional.


4.
The function BODY is surrounded by curly brackets and contains the statements of the function.


5.
The RETURN VALUE is the value that is passed back to the main program. Functions exit whenever you return a value.

This is what a function definition might look like:

#include <stdio.h>

int squareNumber(int a)
{

    int b = a*a;
    return b;

    return 0;

}


squareNumber is the name of this function. Because an integer is returned, the int keyword must be placed before the function name.

If the function does not return a value, we put the void keyword before the function name.

This function has one argument, which is of the type int. If you have arguments, you must put variable declarations in the round brackets.

The function body consists of 2 statements. The first, sees an int variable b declared and assigned a*a, i.e. a squared. The second statement uses the return keyword to pass the value of b back into the main program, hence exiting the function.

Within the program, one might write:

x = squareNumber(5);

This would assign 25 to x. We say that 5 is passed as an argument to the function squareNumber.

The variables within the squareNumber function are LOCAL VARIABLES - when the function exits, variables a and b are deleted from memory.

    #include <stdio.h>

    void printAverage(int x, int y, int z); /* the function declaration */

    int main(void)
    {

        int a, b, c;

        printf("Enter 3 integers separated by spaces: ");
        scanf("%d %d %d", &a, &b, &c);

        printAverage(a, b, c); /* the function call */

        return 0; /* exit main function */

    }

     void printAverage(int x, int y, int z)
    {   

        float average = (float) (x + y + z) / 3;         
        printf("The average value of %d, %d and %d is %f\n", x, y, z, average);

    }

It's common practice to place the function definition underneath main - if you're editing main most of the time, you wouldn't want to scroll too far down the page to get to it!

You can put the function definition above it if you wanted. But if you place it underneath main, make sure you put the function declaration above main - see the example. This is because the computer won't know if the function exists if you call it without it being declared in the first place. It's the same with variables: you can assign anything to x unless you've declared x beforehand.

The function declaration is a single statement consisting of the function header - don't forget the semi colon at the end!

Notice that in the function call I had to pass three arguments to match the three arguments in the function definition. And the variable names in the function's argument section didn't have to match the variable names in the function call. The most important thing is that the data types had to match.

And notice my use of the void keyword for the printAverage function, since no value is returned.

back to top        go to next lesson





 






1