A function is a self-contained block of code that performs a specific task. Functions are used to break large programs into smaller, manageable pieces.

  • Think of a function as a mini-program inside your program.

  • Each function does one specific job, like printing a message, calculating sum, or multiplying numbers.

Benefits of Functions

  1. Reusability – Write once, use multiple times.

  2. Modularity – Divide program into logical sections.

  3. Ease of Maintenance – Change code in one place only.

  4. Readability – Programs are easier to read.

  5. Avoid Code Duplication – Reduces repeated code.

  6. Debugging Made Easy – Errors can be traced in smaller sections.

Function Declaration

  • Function declaration tells the compiler what the function looks like (name, parameters, return type).

  • It is optional for small programs, but necessary for large programs to organize code.

Syntax:

 
return_type function_name(parameter_list);

Example:

 
int add(int, int); // Declaration
  • Here, int → return type

  • add → function name

  • (int, int) → parameters

Function Definition

  • Function definition contains the actual code to perform the task.

  • Can be defined before main() or after main() (if declared first).

Syntax:

 
return_type function_name(parameter_list) {
// statements

}

Example:

 
int add(int a, int b) {
return a + b;
}

Function Call

  • To execute a function, we use a function call.

  • Function call can be in main() or in another function.

Example:

 
int result = add(5, 10);
cout << result;
  • add(5, 10) → passes arguments 5 and 10 to the function.

  • Function returns 15 which is stored in result.

Function Parameters

Functions can take input values (parameters) and may return output.

Types of Parameters

  1. No parameter, no return value

 
void greet() {
cout << "Hello!" << endl;
}
  1. With parameter, no return value

 
void greet(string name) {
cout << "Hello " << name << endl;
}
greet("Ali"); // Output: Hello Ali
  1. No parameter, returns value

 
int getNumber() {
return 10;
}
int n = getNumber(); // n = 10
  1. With parameter, returns value

 
int square(int x) {
return x*x;
}
int sq = square(5); // sq = 25

Call by Value vs Call by Reference

a) Call by Value

  • Function receives a copy of the argument.

  • Changes inside the function do not affect original variable.

 
void changeValue(int x) {
x = x + 5;
}
int main() {
int a = 10;
changeValue(a);
cout << a; // Output: 10
}

b) Call by Reference

  • Function receives original variable using &.

  • Changes inside the function affect the original variable.

 
void changeValue(int &x) {
x = x + 5;
}
int main() {
int a = 10;
changeValue(a);
cout << a; // Output: 15

}

Recursive Functions

  • A recursive function calls itself to solve a problem.

  • Useful for factorial, Fibonacci, and many mathematical problems.

Example: Factorial

 
int factorial(int n) {
if(n == 0)
return 1;
return n * factorial(n-1);
}
int main() {
cout << factorial(5); // Output: 120
}
  • Recursion breaks a big problem into smaller problems.

Advantages of Functions

  1. Reduces Code Complexity – Large programs become simpler.

  2. Easy Testing – Test individual functions.

  3. Efficient Programming – Less code, more clarity.

  4. Collaborative Programming – Teams can work on separate functions.

  5. Supports Recursion – Solves complex problems elegantly.

Function Overloading

  • Functions with same name but different parameters.

  • Allows using one name for multiple tasks.

 
int add(int a, int b) {
return a+b;
}
double add(double a, double b) {
return a+b;
}
cout << add(5, 10); // 15

cout << add(3.5, 2.5); // 6.0

Inline Functions

  • Inline functions execute faster as code is replaced at compile time.

  • Syntax: inline keyword.

 
inline int square(int x) {
return x*x;
}
cout << square(5); // Output: 25
  • Useful for small, frequently used functions.

Default Arguments in Functions

  • Functions can have default parameter values.

 
void greet(string name = "User") {
cout << "Hello " << name << endl;
}
greet(); // Output: Hello User
greet("Ali"); // Output: Hello Ali

Practical Applications of Functions

  1. Mathematical Calculations – Sum, product, factorial, powers.

  2. Input/Output Tasks – Separate functions for input validation, printing menus.

  3. Repetitive Tasks – Reusable blocks instead of writing same code multiple times.

  4. Complex Programs – Break large projects into smaller, manageable functions.

Summary of Functions

  • Functions perform specific tasks and make programs organized.

  • Declaration tells the compiler about the function.

  • Definition contains the actual code.

  • Call by value passes a copy, call by reference passes original variable.

  • Functions can return values or be void.

  • Supports recursion, overloading, inline, and default arguments.

  • Benefits: reusability, modularity, readability, maintenance, and efficiency.