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
int add(int a, int b) { // return type, name, parameters
return a + b; // body: does the work, returns a value
}| Part | Meaning |
|---|---|
int | Return type — the kind of value it gives back |
add | The function's name |
(int a, int b) | Parameters — inputs it receives |
return a + b; | Sends a value back to the caller |
A working example
#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;
}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:
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:
void doubleIt(int &n) { // reference parameter
n = n * 2;
}
int main() {
int x = 5;
doubleIt(x);
cout << x; // 10 — the original changed
}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:
void greet(string name) {
cout << "Hello, " << name << "!\n";
}Common mistakes
- Forgetting the
returnin a non-voidfunction. - 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.
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. voidfunctions perform an action without returning a value.
Function क्या है?
Function एक नामित code block है जो एक काम करता है। आप इसे एक बार लिखते हैं और जब भी वह काम चाहिए तब call करते हैं। Functions programs को व्यवस्थित रखते हैं, दोहराव से बचाते हैं, और code को पढ़ना तथा test करना आसान बनाते हैं।
Function की बनावट
int add(int a, int b) { // return type, naam, parameters
return a + b; // body: kaam karti hai, value lautati hai
}| हिस्सा | अर्थ |
|---|---|
int | Return type — किस तरह की value लौटाता है |
add | Function का नाम |
(int a, int b) | Parameters — जो inputs यह पाता है |
return a + b; | Caller को एक value वापस भेजता है |
एक चलता उदाहरण
#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;
}Sum = 8
Function prototypes
अगर आप function को main के बाद define करना चाहें, तो पहले compiler को उसके बारे में prototype से बताएँ — header और एक semicolon:
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 से भेजें:
void doubleIt(int &n) { // reference parameter
n = n * 2;
}
int main() {
int x = 5;
doubleIt(x);
cout << x; // 10 — mool badal gaya
}10
References बड़े data के लिए कुशल भी हैं, क्योंकि कुछ copy नहीं होता। अधिक के लिए references देखें।
कुछ न लौटाने वाले functions
void तब इस्तेमाल करें जब function बस एक क्रिया करता है:
void greet(string name) {
cout << "Hello, " << name << "!\n";
}आम गलतियाँ
- Non-
voidfunction में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 (
&) मूल साझा करता है। voidfunctions बिना value लौटाए एक क्रिया करते हैं।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में function क्या है?
C++ में function के हिस्से क्या हैं?
int add(int a, int b) में int return type है, add नाम है, और a तथा b parameters हैं जो function call होने पर values पाते हैं।C++ में function prototype क्या है?
int add(int, int);।C++ में pass by value और pass by reference में क्या अंतर है?
& से लिखा जाता है, function को मूल variable पर काम करने देता है, तो बदलाव caller को दिखते हैं।C++ में void function क्या करता है?
void function एक क्रिया करता है पर कोई value नहीं लौटाता। यह तब इस्तेमाल होता है जब काम कुछ करना हो — जैसे संदेश print करना या references के ज़रिए data update करना — बजाय गणना करके परिणाम लौटाने के।