Functions 1: Predefined and Value-Returning Functions

What are Functions?

In general, we use (call) functions (aka: modules, methods, procedures, subprocedures, or subprograms) to perform a specific (atomic) task. In algebra, a function is defined as a rule or correspondence between values, called the function's arguments, and the unique value of the function associated with the arguments. For example:

If f(x) = 2x + 5, then f(1) =  7, f(2) =  9, and f(3) = 11

You would say the call f(1) returns the value 7

1, 2, and 3 are arguments
7, 9, and 11 are the resulting values (or corresponding values)

Bjarne Stroustrup's C++ Glossary
Why use Functions?

Predefined Functions

Using predefined functions:


User-Defined Functions

Using User-Defined functions:

Function prototype includes: Function definition includes:
  1. Header (or heading) includes:
    1. Function name
    2. Number of parameters (if any)
    3. Data type of each parameter
    4. Type of function (data type or void)
  2. Body includes:
    1. Code to accomplish task
    2. ***Any variables (if any) declared in body of function are local to function
    3. All enclosed in braces {}
Function call includes:

Flow of Execution

Program Execution:

Function Example: