06.Functions
Computer Science I COSC 1020
Georgetown University
Functions Overview
Chapter 6: Functions
Key Topics
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)
What is a Function?
Functions encapsulate behavior and enhance modularity.
The UNIX Programming Philosophy
Emphasizes combining simple programs for complex functionality (Kernighan and Pike).
Divide and Conquer Approach
Modular Programming
Break problems into smaller pieces.
Solve and then combine these solutions.
Procedural Programming
Express sub-problems as procedures or functions.
Procedural Programming in C++
Functions are self-contained units with inputs and outputs.
Benefits of Procedural Programming
Source Code Quality
Simplifies debugging and enhances readability.
Reusability
Functions allow code repetition without redundancy (DRY principle).
Abstraction and Encapsulation
Separate what a function does from how it does it, promoting easier updates.
Defining a Function
Functions require declarations and definitions.
Essential components: identifier, parameter list, and return type.
The main()
Function
Is a function itself with specific components including identifiers and a return type.
Calling a Function
Execution pauses on calling; control returns to the caller after the function completes.
Function Locations
Functions can be called in various contexts based on their return type and expected value.
Void-Returning Functions
Do not produce an output value.
Function Calls Producing Output
void print_message() {
cout << "Hello from print_message";
}
Example Output:
"Hello from main"
"Hello from print_message"
"Back again in main"
Function Declarations
Must appear before use.
Verify proper identifier, argument types and numbers.
Function Prototypes
Declare function header without definition.
Inputs and Outputs in Functions
Function Inputs
Functions may require inputs, enhancing their versatility.
Parameter and Argument Terminology
Arguments: values passed during a function call.
Parameters: variables representing those arguments within the function.
Example of Arguments
double root2 = sqrt(2);
double rootn = sqrt(num);
double c = sqrt(a*a + b*b);
Pass-by-Value
Function copies the argument value; original variable remains unchanged.
Function Call Stack
Each function has its own stack frame for handling parameters and local variables.
Reference Variables
Allow sharing memory locations, modifying the original data in the calling context.
Using Reference Variables
Declaration Example
int& reference = number;
Benefits of Pass-by-Reference
Allows functions to modify original variables directly and efficiently share data.
Global Variables
Definition and Scope
Declared outside function, accessible anywhere in the code.
Downsides
Increase complexity and hinder readability.
Static Local Variables
Persist beyond function calls, allowing functions to maintain state without global variables.
Exiting a C++ Program
Normal Exit
Occurs when
main()
returns a value.
Abnormal Exit
Use of
exit()
function to terminate execution without returning control.
Designing with Functions
Menu-driven Programs
Functions enhance program organization, allowing for clearer interfaces.
Stubbing and Driver Functions
Useful in testing function behavior separately before full implementation.
Documenting Functions
Good Practices
Use descriptive function names and comments to clarify purpose and usage.
Default Arguments
Simplifies function calling by allowing omission of common arguments.
Example Declaration
void foo(int a, char b, string c = "", float d = 3.14);
Function Overloading
Different functions can share names so long as their parameter lists differ.
Why Overload?
To provide flexibility in handling different data types with the same operation.