CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Why do we require functions?

The need for, and use of, main() should be clear. However, there's 5 other primary motivations for using functions:

  1. Functions allow us to group together statements that have a strongly related purpose - statements, in combination, performing a single task.

    We prefer to keep such statements together, providing them with a name (as for variables), so that we may refer to the statements, and call them, collectively.

    This provides both convenience and readability.

  2. We often have sequences of statements that appear several times throughout larger programs.

    The repeated sequences may be identical, or very similar (differing only in a very few statements). We group together these similar statement sequences, into a named function, so that we may call the function more than once and have it perform similarly for each call.

    • Historically, we'd identify and group similar statements into functions to minimize the total memory required to hold the (repeated) statements.
    • Today, we use functions not just to save memory, but to enhance the robustness and readability of our code (both good Software Engineering techniques).

  3. From the Systems Programming perspective, the operating system kernel employs functions as well-defined entry points from user-written code into the kernel.

    Such functions are named system calls.

  4. Functions provide a convenient mechanism to package and distribute code. We can distribute code that may be called by other people's code, without providing them with a complete program.

    We frequently use libraries for this purpose.

  5. And, in sufficiently advanced operating systems, multiple running processes can share a single instance of a function, such as printf() - provided that the function's code cannot be modified by any process, and the function makes references to each process's distinct data and the parameters passed to the function.

    Libraries of such functions are often termed shared libraries.

 


CITS2002 Systems Programming, Lecture 4, p2, 1st August 2023.