Module 5: Functions in C++
A function is a reusable block of code that performs a specific task. Functions help organize programs, reduce code duplication, and improve readability.
For example, instead of writing the same addition logic multiple times, you can create an add() function and call it whenever needed.
Function Declaration
A function declaration (also called a function prototype) tells the compiler the function's name, return type, and parameters before it is used.
Syntax
1returnType functionName(parameterList);
Example
1int add(int, int);
Function Definition
A function definition contains the actual implementation of the function.
Example
1#include <iostream> 2using namespace std; 3 4int add(int a, int b) 5{ 6 return a + b; 7} 8 9int main() 10{ 11 cout << add(10, 20); 12 13 return 0; 14}
Output
130
Return Type
The return type specifies what value a function sends back to the caller.
Returning a Value
1int square(int number) 2{ 3 return number * number; 4}
Function Returning Nothing
1void greet() 2{ 3 cout << "Welcome to C++"; 4}
Parameters
Parameters allow values to be passed into a function.
Example
1#include <iostream> 2using namespace std; 3 4void greet(string name) 5{ 6 cout << "Hello, " << name; 7} 8 9int main() 10{ 11 greet("Ankit"); 12 13 return 0; 14}
Output
1Hello, Ankit
Default Arguments
Default arguments provide a value when no argument is supplied during the function call.
Example
1#include <iostream> 2using namespace std; 3 4void greet(string name = "Guest") 5{ 6 cout << "Hello, " << name; 7} 8 9int main() 10{ 11 greet(); 12 cout << endl; 13 greet("Ankit"); 14 15 return 0; 16}
Output
1Hello, Guest 2Hello, Ankit
Inline Functions
An inline function suggests to the compiler to replace the function call with the function's code, reducing function call overhead for small functions.
Example
1#include <iostream> 2using namespace std; 3 4inline int square(int x) 5{ 6 return x * x; 7} 8 9int main() 10{ 11 cout << square(5); 12 13 return 0; 14}
Output
125
Recursive Functions
A recursive function calls itself until a stopping condition (base case) is reached.
Example: Factorial
1#include <iostream> 2using namespace std; 3 4int factorial(int n) 5{ 6 if (n == 0 || n == 1) 7 return 1; 8 9 return n * factorial(n - 1); 10} 11 12int main() 13{ 14 cout << factorial(5); 15 16 return 0; 17}
Output
1120
Function Overloading
Function overloading allows multiple functions to have the same name but different parameter lists.
Example
1#include <iostream> 2using namespace std; 3 4int add(int a, int b) 5{ 6 return a + b; 7} 8 9double add(double a, double b) 10{ 11 return a + b; 12} 13 14int main() 15{ 16 cout << add(10, 20) << endl; 17 cout << add(5.5, 2.5); 18 19 return 0; 20}
Output
130 28
Lambda Functions
A lambda function is an anonymous function introduced in C++11. It is useful for short operations that don't require a named function.
Syntax
1[capture](parameters) 2{ 3 // Function body 4};
Example
1#include <iostream> 2using namespace std; 3 4int main() 5{ 6 auto square = [](int x) 7 { 8 return x * x; 9 }; 10 11 cout << square(6); 12 13 return 0; 14}
Output
136
Comparison of Function Types
| Feature | Description |
|---|---|
| Function Declaration | Introduces the function to the compiler |
| Function Definition | Contains the implementation |
| Return Type | Specifies the returned value |
| Parameters | Accept input values |
| Default Arguments | Provide default parameter values |
| Inline Function | Improves performance for small functions |
| Recursive Function | Calls itself repeatedly |
| Function Overloading | Same function name with different parameters |
| Lambda Function | Anonymous function for short tasks |
Best Practices
- Use descriptive function names.
- Keep each function focused on a single task.
- Use
constparameters when values should not change. - Avoid unnecessary recursion for simple problems.
- Use function overloading only when functions perform related tasks.
- Prefer lambda functions for short, local operations.
Practice Exercises
- Create a function to add two numbers.
- Write a function to calculate the area of a rectangle.
- Create a recursive function to calculate Fibonacci numbers.
- Demonstrate function overloading for different data types.
- Write a lambda function to calculate the square of a number.
Interview Questions
- What is a function in C++?
- What is the difference between a function declaration and definition?
- What is a return type?
- What are function parameters?
- What are default arguments?
- What is an inline function?
- What is recursion? What is a base case?
- What is function overloading?
- What are lambda functions, and when should you use them?
Summary
In this module, you learned:
- How to declare and define functions.
- How return types and parameters work.
- How to use default arguments.
- The purpose of inline functions.
- How recursive functions solve repetitive problems.
- How function overloading allows multiple functions with the same name.
- How lambda functions provide concise, anonymous functions.
In the next module, you'll learn Arrays in C++, including one-dimensional arrays, multidimensional arrays, character arrays, array traversal, searching, sorting, and common array algorithms.