Todayโs Outline
- Defining a Function
- Calling a Function
- Declare a Function
- Passing Parameters
- Recursive Functions
Defining a Function
Calling a Function
Function in C++ Library
#include <cmath>
- Mathematical Calculations
#include <cstring>
- String Manipulations
#include <iostream>
- Input/Output
Function in Memory Stack
Declare a Function
Function Prototype
- The function must be declared before use
- The function prototype is the declaration of the function
- The implementation of the function can be defined later
- Contains function prototype only
- To be included in the program that will call the function
Implementation file (.cpp)
- Contains function implementation
Passing Parameters
Argument vs Parameter
Parameter Passing in C++
- Pass-by-Value
- Pass-by-Reference
- Pass-by-Pointer (Later)
Pass-by-Value
Pass-by-Reference
Parameter Passing: Default Parameters
- Default parameters must be defined from the last parameter
Recursive Functions
Iterative vs Recursive
Efficiency of Recursion
- Generally speaking, non-recursive versions will execute more efficiently (time/space)
Summary
- Define, call, and declare functions
- Parameter passing
- Recursive functions