🟡 Control Flow  ·  Lesson 23

Functions in C++

What is a function?

A function is a named block of code that does one job. You write it once and call it whenever you need that job done. Functions keep programs organised, avoid repetition, and make code easier to read and test.

Anatomy of a function

C++
int add(int a, int b) {   // return type, name, parameters
    return a + b;         // body: does the work, returns a value
}
PartMeaning
intReturn type — the kind of value it gives back
addThe function's name
(int a, int b)Parameters — inputs it receives
return a + b;Sends a value back to the caller

A working example

C++
#include <iostream>
using namespace std;

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

int main() {
    int result = add(3, 5);   // call the function
    cout << "Sum = " << result << "\n";
    return 0;
}
Output:
Sum = 8

Function prototypes

If you want to define a function after main, you tell the compiler about it first with a prototype — the header plus a semicolon:

C++
int add(int, int);        // prototype

int main() {
    cout << add(2, 4);     // works, thanks to the prototype
}

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

Pass by value vs reference

By default a function gets a copy of each argument, so it cannot change the caller's variable. Add & to pass by reference and work on the original:

C++
void doubleIt(int &n) {   // reference parameter
    n = n * 2;
}
int main() {
    int x = 5;
    doubleIt(x);
    cout << x;            // 10 — the original changed
}
Output:
10

References are also efficient for large data, since nothing is copied. See references for more.

Functions that return nothing

Use void when the function just performs an action:

C++
void greet(string name) {
    cout << "Hello, " << name << "!\n";
}

Common mistakes

  • Forgetting the return in a non-void function.
  • Calling a function before declaring or prototyping it.
  • Mismatching argument types or count against the parameters.
  • Expecting a pass-by-value function to change the caller's variable.
🏋️ Practice

Write a function maxOf(int, int) that returns the larger of two numbers, and a void function that prints a line of stars of a given length. Call both from main.

Summary

  • A function is a reusable named block that does one job.
  • It has a return type, name, parameters and a body.
  • Prototypes let you define a function after it is called.
  • Pass by value copies; pass by reference (&) shares the original.
  • void functions perform an action without returning a value.

Function क्या है?

Function एक नामित code block है जो एक काम करता है। आप इसे एक बार लिखते हैं और जब भी वह काम चाहिए तब call करते हैं। Functions programs को व्यवस्थित रखते हैं, दोहराव से बचाते हैं, और code को पढ़ना तथा test करना आसान बनाते हैं।

Function की बनावट

C++
int add(int a, int b) {   // return type, naam, parameters
    return a + b;         // body: kaam karti hai, value lautati hai
}
हिस्साअर्थ
intReturn type — किस तरह की value लौटाता है
addFunction का नाम
(int a, int b)Parameters — जो inputs यह पाता है
return a + b;Caller को एक value वापस भेजता है

एक चलता उदाहरण

C++
#include <iostream>
using namespace std;

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

int main() {
    int result = add(3, 5);   // function ko call karein
    cout << "Sum = " << result << "\n";
    return 0;
}
Output:
Sum = 8

Function prototypes

अगर आप function को main के बाद define करना चाहें, तो पहले compiler को उसके बारे में prototype से बताएँ — header और एक semicolon:

C++
int add(int, int);        // prototype

int main() {
    cout << add(2, 4);     // prototype ki wajah se kaam karta hai
}

int add(int a, int b) {   // definition, baad mein
    return a + b;
}

Pass by value बनाम reference

Default रूप से function हर argument की copy पाता है, तो वह caller का variable नहीं बदल सकता। मूल पर काम करने को & जोड़कर reference से भेजें:

C++
void doubleIt(int &n) {   // reference parameter
    n = n * 2;
}
int main() {
    int x = 5;
    doubleIt(x);
    cout << x;            // 10 — mool badal gaya
}
Output:
10

References बड़े data के लिए कुशल भी हैं, क्योंकि कुछ copy नहीं होता। अधिक के लिए references देखें।

कुछ न लौटाने वाले functions

void तब इस्तेमाल करें जब function बस एक क्रिया करता है:

C++
void greet(string name) {
    cout << "Hello, " << name << "!\n";
}

आम गलतियाँ

  • Non-void function में return भूलना।
  • Function को declare या prototype करने से पहले call करना।
  • Arguments के types या संख्या को parameters से बेमेल करना।
  • Pass-by-value function से caller का variable बदलने की उम्मीद करना।
🏋️ अभ्यास

एक function maxOf(int, int) लिखें जो दो numbers में से बड़ा लौटाए, और एक void function जो दी गई लंबाई के stars की एक line print करे। दोनों को main से call करें।

सारांश

  • Function एक पुनः-प्रयोग योग्य नामित block है जो एक काम करता है।
  • इसमें एक return type, नाम, parameters और body होती है।
  • Prototypes आपको function को call के बाद define करने देते हैं।
  • Pass by value copy करता है; pass by reference (&) मूल साझा करता है।
  • void functions बिना value लौटाए एक क्रिया करते हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में function क्या है?
Function एक नामित code block है जो एक विशिष्ट काम करता है और ज़रूरत पड़ने पर call किया जा सकता है। यह program को छोटे, पुनः-प्रयोग योग्य टुकड़ों में बाँटने में मदद करता है, तो code दोहराने के बजाय आप उसे एक बार function में लिखकर नाम और ज़रूरी arguments के साथ call करते हैं।
C++ में function के हिस्से क्या हैं?
Function में एक return type, एक नाम, कोष्ठकों में parameter सूची, और braces में body होती है। जैसे int add(int a, int b) में int return type है, add नाम है, और a तथा b parameters हैं जो function call होने पर values पाते हैं।
C++ में function prototype क्या है?
Prototype किसी function का नाम, return type और parameters उसके इस्तेमाल से पहले declare करता है, ताकि compiler जाने कि उसे कैसे call करना है भले पूरी definition file में बाद में आए। यह function header के बाद semicolon की तरह लिखा जाता है, जैसे int add(int, int);
C++ में pass by value और pass by reference में क्या अंतर है?
Pass by value function को argument की copy देता है, तो function के अंदर बदलाव caller के variable को प्रभावित नहीं करते। Pass by reference, parameter में & से लिखा जाता है, function को मूल variable पर काम करने देता है, तो बदलाव caller को दिखते हैं।
C++ में void function क्या करता है?
void function एक क्रिया करता है पर कोई value नहीं लौटाता। यह तब इस्तेमाल होता है जब काम कुछ करना हो — जैसे संदेश print करना या references के ज़रिए data update करना — बजाय गणना करके परिणाम लौटाने के।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.