Writing Functions

A function is a named, independent section of C code that performs a specific task and optionally returns a value to the calling program
Benefits of the functions

  • They break large computing into smaller ones
  • It avoids the need for redundant programming
  • It increases the logical clarity of the program
  • It also promotes portability

Function Terminology:

  • Function prototype: A function’s prototype contains the name of the function, return type, a list of variables that must be passed to it, and the type of variable it returns, if any..
  • Function Definition: The function itself is called the function definition. The definition contains the code that will be executed.
  • Function header: The function header is at the start of a function, and it gives the function’s name. The header also gives the function’s return type and describes its arguments. Note that the function header is identical to the function prototype (minus the semicolon).
  • Function Body: The statements written within function are called function body. Statements are enclosed in braces. These statements are executed when the function is called.
  • Arguments: When a function is called, the program can send the function information in the form of one or more arguments. An argument is program data needed by the function to perform its task.Arguments are enclosed in parentheses following the function’s name.
  • Return statement: If the function return type is anything other than void, a return statement should be included, returning a value matching the return type.

How does a Function Works:

    • When a function is called, the program can send the function information in the form of one or more arguments. An argument is program data needed by the function to perform its task. The statements in the function then execute, performing whatever task each was designed to do.
    • When the function’s statements have finished, execution passes back to the same location in the program that called the function. Functions can send information back to the program in the form of a return value.