CITS2002 Systems Programming  
next CITS2002 CITS2002 schedule  

Introducing functions

C is a procedural programming language, meaning that its primary synchronous control flow mechanism is the procedure call.

C names its procedures functions (in contrast, Java has a different mechanism - methods).

  • In Mathematics, we apply a function, such as the trigonometric function cos, to one or more values.
    The function performs an evaluation, and returns a result.

  • In many programming languages, including C, we call or invoke a function.
    We evaluate zero or more expressions, the result of each expression is copied to a memory location where the function can receive them as arguments, the function's statements are executed (often involving the arguments), and a result is returned (unless the function is stuck in an infinite-loop or exits the process!)

We've already seen the example of main() - the function that all C programs must have, which we might write in different ways:

#include <stdio.h>
#include <stdlib.h>

int main(int argcount, char *argvalue[])
{
    // check the number of arguments
    if(argcount != 2)
    {
        ....
        exit(EXIT_FAILURE);
    }
    else
    {
        ....
        exit(EXIT_SUCCESS);
    }
    return 0;
}



#include <stdio.h>
#include <stdlib.h>

int main(int argcount, char *argvalue[])
{
    int result;

    // check the number of arguments
    if(argcount != 2)
    {
        ....
        result = EXIT_FAILURE;
    }
    else
    {
        ....
        result = EXIT_SUCCESS;
    }
    return result;
}

The operating system calls main(), passing to it some (command-line) arguments, main() executes some statements, and returns to the operating system a result - usually EXIT_SUCCESS or EXIT_FAILURE.

 


CITS2002 Systems Programming, Lecture 5, p1, 12th August 2019.