Functions in C++

By Shakib Ansari | Date: Sun, Jun 1, 2025

Functions in C++

Programming mein functions ek important concept hain jo aapke code ko organized, reusable, aur clean banate hain. Aaj hum C++ ke functions ka poora deep dive karenge with real examples.

1. Function Declaration and Definition

Declaration (Prototype)

Function banane se pehle compiler ko batana padta hai ke function exist karta hai – isse kehte hain declaration ya function prototype.

int add(int, int)// Declaration

Definition

Yahaan hum batate hain ki function ka code kya karega.

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

Complete Example:

#include<iostream>
using namespace std;

// Declaration
int add(int, int);

int main() {
    int result = add(5, 3);
    cout << "Sum is: " << result;
    return 0;
}
// Definition
int add(int a, int b) {
    return a + b;
}

2. Parameters – Pass by Value vs Reference

Pass by Value

By default, C++ mein arguments copy hoke function mein jaate hain. Original value change nahi hoti.

void change(int x) {
  x = x + 5;
}

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

Pass by Value

Jab aap & use karte ho, toh original variable ka address pass hota hai. Changes directly original value par effect karte hain.

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

Tip: Use pass-by-reference jab aapko original value change karni ho, ya performance chahiye (large data types ke liye).

3. Recursion – Function Calling Itself

Recursion ka matlab hota hai ki function khud ko hi call kare – jab tak ek base condition meet na ho jaaye.

Factorial Using Recursion:

int factorial(int n) {
  if (n == 0) return 1;
  return n * factorial(n - 1);
}

int main() {
  cout << factorial(5); // Output: 120
}

Tip: Har recursive function ke paas ek base condition zaroor honi chahiye, warna infinite loop ban sakta hai.

4. Function Overloading

C++ mein aap same naam ke multiple functions bana sakte ho, bas unke parameters alag hone chahiye. Is concept ko kehte hain Function Overloading.

Example:

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

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

int main() {
    cout << sum(5, 2);         // Calls int version
    cout << sum(3.5f, 2.5f);   // Calls float version
} 

Compiler input ke hisaab se correct version choose karta hai.

5. Inline Functions – Small but Fast

Agar aap chhote functions ke liye performance optimize karna chahte ho, toh use karo inline keyword. Yeh compiler ko suggest karta hai ki function call ko directly code se replace kar de.

Example:

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

int main() {
    cout << square(5);  // Output: 25
}

Use inline only for small and frequently used functions. Bade functions mein use karna memory waste kar sakta hai.

Bonus: Practice Exercises

  1. Ek function banao jo 2 numbers ka maximum return kare.
  2. Ek recursive function likho jo nth Fibonacci number calculate kare.
  3. Ek overloaded function banao jo integer aur float sum kare.
  4. Inline function likho jo cube calculate kare.
About the Author

Hi, I'm Shakib Ansari, Founder and CEO of BeyondMan. I'm a highly adaptive developer who quickly learns new programming languages and delivers innovative solutions with passion and precision.

Shakib Ansari
Programming

Comments