Tech

Functions–STRUCTURED PROGRAMMING Course Notes

Functions

Experience has shown that the best way to develop & maintain a program is to construct it from small, simple pieces or components. This technique is called “divide & conquer”.

Declare and use functions to facilitate the design, implementation, operation, and maintenance of large programs. Functions (aka methods, procedures, sub-routines) allow us to modularize a program by separating its tasks into self-contained units.

We can use a combination of provided library functions or create out own functions, known as user-defined, or programmer-defined functions.

The statements in function bodies are written only once but can be reused from perhaps several locations in a program and are hidden from other functions.

Functions also allow us to avoid repeating code.

And functions make the program easier to debug & maintain.

*To promote software reusability, every function should be limited to performing a single, well-defined task, and the name of the function should express that task effectively.

Functions that are members of a class are called “member functions”.

Functions that are NOT members of a class are called “global functions“.

Like a class’s member functions, the function prototype for global functions are placed in header files, so that the global functions can be reused in any program that includes the header file, and that can link to the functions object code.

For example: to use the function ‘pow’ we include the <cmath> header file to raise a value to a power.

Note: There’s no need to create any objects before calling global functions, in the <cmath> header, because all functions in the <cmath> header are global functions; Therefore, each is called simply by specifying the name of the function followed by parenthesis containing the functions arguments, Ex: sqrt(900.0), or how we simply call the ‘main’ function with: main().

An important feature of function prototypes is argument coercion–i.e., forcing arguments to the appropriate types specified by the parameter declarations.

Arguments can be promoted by the compiler to the parameter types as specified by C++’s promotion rules. The promotion rules indicate how to convert between types without losing data.

  • Function Prototype (aka a function declaration)
    • tells the compiler the name of a function, the type of data returned by the function, the number of parameters the function expects to receive, the types of those parameters and the order in which the parameters of those types are expected.
  • Function signature (or just ‘signature‘) is the portion of a function prototype that includes the name of the function & the types of its arguments.
  • Note: Function Prototypes are REQUIRED! Use #include preprocessor directives to “include” them. The C++ Standard Library is divided into many portions, each with its own header file. The header files contain the function prototypes for the related functions that form each portion of the library. (The header files also contain definitions of various class types, functions & constants.)

*A header file “instructs” the compiler on how to interface with library components, an user-written ones.

There are 3 ways to return control to the point at which a function was invoked:

  • If the function does not return a result (i.e., it has a void return type), control returns when the program reaches the function-ending right brace,
  • or by execution of the statement:
    • return;
  • If the function does return a result, the statement:
    • return expression;
    • evaluates ‘expression‘ and returns the value of ‘expression‘ to the caller.