Chapter 2: Working on Functions – Detailed and Simplified Summary
This chapter focuses on functions in C++, providing foundational and advanced knowledge
about their purpose, definition, declaration, and usage. It highlights modular programming
concepts and covers advanced features like recursion, inline functions, and overloading. Below is
a detailed breakdown.
1. Functions and Their Role
What is a Function?
A function is a block of reusable code designed to perform a specific task. Functions in
C++ help to modularize a program, improving readability, maintainability, and
debugging. Every program starts with the main() function.
Types of Functions in C++:
1. Built-in Functions: Predefined functions in libraries (e.g., pow() and sqrt()
from <cmath>).
2. User-defined Functions: Written by the programmer to handle specific tasks.
2. Modular Programming
Definition: Breaking down a large program into smaller, independent modules.
Each module handles a distinct functionality.
Modules can be tested independently, reducing overall complexity.
Supports abstraction by hiding implementation details.
3. Key Components of a Function
3.1 Function Declaration (Prototype)
A function declaration tells the compiler:
o The name of the function.
o The return type.
o The parameters (signature).
It does not include the function body.
Syntax:
returnType functionName(parameter_list);
Example:
double totalCost(int quantity, double price);
3.2 Function Definition
Includes the function header and function body, defining the logic.
Example:
double totalCost(int quantity, double price) {
return quantity * price;
}
3.3 Function Call
To execute a function, you call it by name and pass the required arguments.
Example:
int result = totalCost(10, 20.5);
4. Types of Function Arguments
Actual and Formal Arguments
Actual Arguments: Values passed to the function during the call.
Formal Arguments: Variables defined in the function header to accept these values.
Call by Value vs. Call by Reference
1. Call by Value:
o A copy of the argument is passed.
o Changes made in the function do not affect the original variable. Example:
2. void doubleValue(int x) {
3. x = x * 2; // Only modifies the local copy
4. }
5. Call by Reference:
o Passes the actual variable (using &).
o Changes in the function affect the original variable. Example:
6. void doubleValue(int &x) {
7. x = x * 2; // Modifies the original variable
8. }
5. Advanced Function Features
5.1 Default Arguments
Allows parameters to have default values if not explicitly provided during the call.
Example:
int divide(int a, int b = 2) {
return a / b;
}
Function calls:
divide(10) → Uses default value of b (2).
divide(10, 5) → Overrides default value of b.
5.2 Inline Functions
Small functions marked with inline are expanded directly at the call site, avoiding
function overhead. Example:
inline int square(int x) {
return x * x;
}
5.3 Recursive Functions
A function that calls itself, directly or indirectly.
Common examples include factorial and Fibonacci series.
Factorial Example:
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
Fibonacci Example:
int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
5.4 Function Overloading
Allows multiple functions with the same name but different parameter lists. Example:
float area(int radius) {
return 3.14 * radius * radius; // Circle
}
int area(int length, int breadth) {
return length * breadth; // Rectangle
}
6. Key Examples and Programs
Basic User-defined Function
#include <iostream>
using namespace std;
int sum(int a, int b); // Declaration
int main() {
int x = 5, y = 10;
cout << "Sum: " << sum(x, y); // Function Call
return 0;
}
int sum(int a, int b) { // Definition
return a + b;
}
Call by Value Example
void func(int num) {
num = num * 2;
cout << "Inside function: " << num << endl;
}
int main() {
int x = 10;
func(x);
cout << "Outside function: " << x << endl;
}
Call by Reference Example
void func(int &num) {
num = num * 2;
}
int main() {
int x = 10;
func(x);
cout << "Modified value: " << x << endl;
}
Recursive Factorial
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
Inline Function for Circle Area
#include <iostream>
using namespace std;
inline float circleArea(int radius) {
return 3.14 * radius * radius;
}
int main() {
cout << "Area: " << circleArea(5);
return 0;
}
7. Exercises
1. Write a function to calculate:
o Factorial of a number.
o Fibonacci series.
o Area of a triangle or rectangle.
2. Implement swapping two numbers using call by value and call by reference.
Conclusion
Functions are fundamental in C++ for creating modular, efficient, and reusable code. This
chapter covered their definition, types, argument passing mechanisms, and advanced features
like recursion, overloading, and inline usage, along with examples to strengthen understanding.