Lesson 15
Functions
Covers declaration, parameters, return, recursion.
Lesson content
What are functions?
Function is named code block for one task.
Write once. Call it whenever needed.
Coffee machine analogy. Press button, get coffee.
Why do functions exist?
- Reduce code duplication
- Improve readability
- Simplify debugging
- Make programs easier to maintain
- Encourage code reuse
Real-world use cases
- Banking applications
- Calculator programs
- Game development
- Student management systems
- File processing
- Scientific calculations
- Web applications
- Data processing software
Core concepts
- Function declaration (prototype)
- Function definition
- Function call
- Return value (optional)
Function declaration (prototype)
Tells compiler about function before use.
return_type functionName(parameter_list);
int add(int, int);Function definition
Holds the actual implementation.
return_type functionName(parameters)
{
// Function body
}
int add(int a, int b)
{
return a + b;
}Function call
int result = add(10, 20);Function parameters
Parameters receive values passed in.
void greet(string name)Passed value is called an argument.
Return type
return sends a value back.
double calculateArea(double radius)
{
return 3.14159 * radius * radius;
}Use void if function returns nothing.
Types of functions
Type | Description
------------------------------ | -----------------------------------
No parameters, no return value | Runs a task, no input or output
Parameters, no return value | Takes input, returns nothing
No parameters, returns value | Returns result, no input
Parameters and return value | Takes input, returns a resultFunction scope
Local vars exist only inside their function.
Recursion
Recursive function calls itself for smaller subproblems.
Must have a base case to stop.
Example 1: Function without parameters
#include <iostream>
using namespace std;
void greet()
{
cout << "Welcome to C++!";
}
int main()
{
greet();
return 0;
}Example 2: Function with parameters
#include <iostream>
using namespace std;
void displaySum(int a, int b)
{
cout << a + b;
}
int main()
{
displaySum(15, 20);
return 0;
}Example 3: Function returning a value
#include <iostream>
using namespace std;
int square(int number)
{
return number * number;
}
int main()
{
int result = square(8);
cout << result;
return 0;
}Example 4: Finding the largest number
#include <iostream>
using namespace std;
int largest(int a, int b)
{
if (a > b)
{
return a;
}
return b;
}
int main()
{
cout << largest(45, 72);
return 0;
}Example 5: Recursive factorial
#include <iostream>
using namespace std;
int factorial(int number)
{
if (number <= 1)
{
return 1;
}
return number * factorial(number - 1);
}
int main()
{
cout << factorial(5);
return 0;
}Common mistakes and pitfalls
- Calling before declaring. Fix: declare first
- Missing return in non-void. Fix: return a value
- Wrong argument count. Fix: match parameters
- Local vars used outside scope. Fix: keep them inside
- Infinite recursion. Fix: add a base case
// Wrong
int square(int number)
{
number * number;
}// Correct
int square(int number)
{
return number * number;
}Best practices
- Each function does one task
- Use descriptive function names
- Avoid overly long functions
- Pass only needed parameters
- Return values over global variables
- Reuse functions, avoid duplication
When not to use this
- Functions doing too many tasks
- Tiny functions adding complexity
- Excessive nested function calls
- Recursion where loops fit better
Summary
- Functions split programs into reusable parts
- Can take params, return values, both, neither
- Declaration informs compiler beforehand
- Definition holds the real code
- Call runs the function
- Local vars stay in their function
- Recursion needs a base case
- Good functions aid readability and reuse
Frequently asked questions
What is a function in C++?
Named code block doing a specific task.
What is the difference between a parameter and an argument?
Parameter is declared. Argument is passed value.
What is the purpose of the return statement?
Ends function, may send back a value.
What is recursion?
Function calling itself on smaller subproblems.
When should I use void functions?
Use when function acts but returns nothing.