⬅ Previous Next ➡

Functions in C++

Functions in C++
  • Function is a reusable block of code that performs a specific task.
  • C++ supports advanced features like function overloading, inline functions, and default arguments.
  • Arguments can be passed using call by value or call by reference.

1) Function Declaration & Definition

  • Declaration (prototype) tells the compiler function name, return type, and parameters.
  • Definition contains the actual function code.
#include <iostream>
using namespace std;

// Declaration (prototype)
int add(int a, int b);

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

int main() {
    cout << "Sum = " << add(10, 20) << endl;
    return 0;
}

2) Function Overloading

  • Same function name with different parameter list (number/type/order).
  • Return type alone cannot overload a function.
#include <iostream>
using namespace std;

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

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

int sum(int a, int b, int c) {
    return a + b + c;
}

int main() {
    cout << sum(10, 20) << endl;
    cout << sum(2.5, 3.5) << endl;
    cout << sum(1, 2, 3) << endl;
    return 0;
}

3) Inline Functions

  • inline suggests compiler to replace function call with function body (good for small functions).
  • May increase program size if overused.
#include <iostream>
using namespace std;

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

int main() {
    cout << "Square = " << square(5) << endl;
    return 0;
}

4) Default Arguments

  • Allows setting default values for parameters.
  • Defaults are written in function declaration (or first definition).
  • Default arguments must be from right to left.
#include <iostream>
using namespace std;

int bill(int price, int qty = 1) {
    return price * qty;
}

int main() {
    cout << "Bill1 = " << bill(100) << endl;      // qty default = 1
    cout << "Bill2 = " << bill(100, 3) << endl;   // qty = 3
    return 0;
}

5) Call by Value

  • Function receives a copy of the value.
  • Changes inside function do not affect original variable.
#include <iostream>
using namespace std;

void changeVal(int x) {
    x = 100;
}

int main() {
    int a = 10;
    changeVal(a);
    cout << "a = " << a << endl; // still 10
    return 0;
}

6) Call by Reference (Using Reference Variables)

  • Function receives reference (alias) of variable using &.
  • Changes inside function affect original variable.
#include <iostream>
using namespace std;

void changeVal(int &x) {
    x = 100;
}

int main() {
    int a = 10;
    changeVal(a);
    cout << "a = " << a << endl; // now 100
    return 0;
}

7) Swap using Call by Reference

#include <iostream>
using namespace std;

void swapNum(int &a, int &b) {
    int t = a;
    a = b;
    b = t;
}

int main() {
    int x = 5, y = 9;

    cout << "Before: " << x << " " << y << endl;
    swapNum(x, y);
    cout << "After : " << x << " " << y << endl;

    return 0;
}

8) Quick Notes

  • Overloading improves readability by using same name for similar tasks.
  • Inline is best for small frequently used functions.
  • Default arguments simplify function calls.
  • Call by reference is used when original value must be modified.
⬅ Previous Next ➡