C++ Functions Explained | Declaration, Definition, Overloading and Call by Reference


This complete tutorial on C++ Functions explains how functions are created and used in C++ programs. It covers function declaration and definition, function calls, return types, function overloading, inline functions, call by value, and call by reference. The tutorial follows best coding practices and helps beginners write modular, reusable, and efficient C++ code.

Functions – Complete Tutorial

1. What is a Function?

A function is a block of code that performs a specific task. Functions help in code reuse, modularity, and readability.

2. Function Declaration and Definition

Function Declaration

A function declaration tells the compiler about the function name, return type, and parameters.

Syntax:


return_type function_name(parameters);

Example:


int add(int, int);

Function Definition

A function definition contains the actual implementation.

Example:


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

3. Function Call

A function is executed when it is called.

Example:


int result = add(5, 3);
cout << result;

4. Return Types

The return type specifies the type of value a function returns.

Common Return Types:

  1. int
  2. float
  3. double
  4. char
  5. void

Example:


void display() {
cout << "Hello";
}

Best Practices:

  1. Use meaningful return types
  2. Avoid using global variables instead of return values

5. Function Overloading

Function overloading allows multiple functions with the same name but different parameter lists.

Example:


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

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

Rules:

  1. Different number of parameters or
  2. Different types of parameters
  3. Return type alone cannot differentiate functions

6. Inline Functions

Inline functions reduce function call overhead by replacing the function call with the function body.

Syntax:


inline int square(int x) {
return x * x;
}

Best Practices:

  1. Use inline for small functions
  2. Avoid inline for complex or recursive functions

7. Call by Value

In call by value, a copy of the actual parameter is passed to the function.

Example:


void change(int x) {
x = 20;
}

int main() {
int a = 10;
change(a);
cout << a; // Output: 10
}

8. Call by Reference

In call by reference, the function receives a reference to the actual parameter.

Example:


void change(int &x) {
x = 20;
}

int main() {
int a = 10;
change(a);
cout << a; // Output: 20
}

Advantages:

  1. No extra memory used
  2. Faster execution
  3. Used to modify original values

Call by Value vs Call by Reference

FeatureCall by ValueCall by Reference
Data copiedYesNo
Memory usageMoreLess
Original value modifiedNoYes

Best Practices for Functions

  1. Keep functions small and focused
  2. Use meaningful function names
  3. Avoid side effects
  4. Prefer passing by reference for large objects
  5. Use const reference when modification is not required

Common Mistakes to Avoid

  1. Mismatched function declaration and definition
  2. Overusing inline functions
  3. Confusing overloading with overriding
  4. Passing large objects by value unnecessarily

Summary

In this chapter, you learned about C++ functions, including declaration, definition, function calls, return types, function overloading, inline functions, and parameter passing techniques. Functions are essential for building modular and maintainable C++ programs.