Georgetown University
What’s a function?
Modular programming (6.1)
Defining and calling functions (6.2)
Function prototypes (6.3)
Function inputs and outputs
Sending data into functions (6.4)
Pass-by-value (6.5)
Reference variable params (6.13)
Arrays as function arguments (8.9)
Return statements (6.6-8)
Functions, scope, and lifetime
Local and global variables (6.10)
Static variables (6.11)
The exit() function
Designing with functions
Menu-driven programs (6.9)
Stubs and drivers (6.16)
Documenting functions
Default arguments (6.12)
Overloading functions (6.13)
Functions encapsulate behavior and enhance modularity.
Emphasizes combining simple programs for complex functionality (Kernighan and Pike).
Break problems into smaller pieces.
Solve and then combine these solutions.
Express sub-problems as procedures or functions.
Functions are self-contained units with inputs and outputs.
Simplifies debugging and enhances readability.
Functions allow code repetition without redundancy (DRY principle).
Separate what a function does from how it does it, promoting easier updates.
Functions require declarations and definitions.
Essential components: identifier, parameter list, and return type.
main()
FunctionIs a function itself with specific components including identifiers and a return type.
Execution pauses on calling; control returns to the caller after the function completes.
Functions can be called in various contexts based on their return type and expected value.
Do not produce an output value.
void print_message() {
cout << "Hello from print_message";
}
"Hello from main"
"Hello from print_message"
"Back again in main"
Must appear before use.
Verify proper identifier, argument types and numbers.
Declare function header without definition.
Functions may require inputs, enhancing their versatility.
Arguments: values passed during a function call.
Parameters: variables representing those arguments within the function.
double root2 = sqrt(2);
double rootn = sqrt(num);
double c = sqrt(a*a + b*b);
Function copies the argument value; original variable remains unchanged.
Each function has its own stack frame for handling parameters and local variables.
Allow sharing memory locations, modifying the original data in the calling context.
int& reference = number;
Allows functions to modify original variables directly and efficiently share data.
Declared outside function, accessible anywhere in the code.
Increase complexity and hinder readability.
Persist beyond function calls, allowing functions to maintain state without global variables.
Occurs when main()
returns a value.
Use of exit()
function to terminate execution without returning control.
Functions enhance program organization, allowing for clearer interfaces.
Useful in testing function behavior separately before full implementation.
Use descriptive function names and comments to clarify purpose and usage.
Simplifies function calling by allowing omission of common arguments.
void foo(int a, char b, string c = "", float d = 3.14);
Different functions can share names so long as their parameter lists differ.
To provide flexibility in handling different data types with the same operation.