C++ Function के Parameters
In this page:
return_type function_name(type param) { // pass by value
// body
}
return_type function_name(type ¶m) { // pass by reference
// body
}
Value से Pass करना
Value से pass करना function को आपके दिए किसी भी argument की अपनी independent copy देता है, इसलिए function के अंदर parameter में किए गए कोई भी changes caller में original variable पर zero असर डालते हैं।
यह सबसे safe default है, क्योंकि caller का data unintended modification से पूरी तरह protected है।
उदाहरण: Pass by Value
// Include std::cout and std::cin
#include <iostream>
void increment(int x) {
x = x + 1;
}
// Program execution starts in main()
int main() {
// Declare num and set it to 5
int num = 5;
increment(num);
// Print to the console with cout
std::cout << num << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reference से Pass करना
void update(int &x) जैसे parameter type में & इस्तेमाल करके reference से pass करना function को एक copy की बजाय खुद original variable तक direct access देता है — function के अंदर किया कोई भी change caller को भी तुरंत visible होता है।
यह essential है जब भी एक function को actually caller का data modify करना हो, जैसे दो values swap करना।
उदाहरण: Pass by Reference
// Include std::cout and std::cin
#include <iostream>
void update(int &x) {
x = x + 1;
}
// Program execution starts in main()
int main() {
// Declare num and set it to 5
int num = 5;
update(num);
// Print to the console with cout
std::cout << num << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Const Parameters
const को एक reference parameter के साथ combine करना, जैसे void print(const string &name), आपको reference passing का performance benefit देता है (कोई copy नहीं बनती, जो strings जैसे बड़े objects के लिए मायने रखता है) साथ ही guarantee करते हुए कि function accidentally caller का data modify नहीं कर सकता।
यह pattern exactly इसी कारण real C++ code में बहुत common है।
उदाहरण: Const Parameters
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
void print(const std::string &name) {
// Print to the console with cout
std::cout << "Hello, " << name << std::endl;
}
// Program execution starts in main()
int main() {
print("Alex");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कई Parameters
एक function को ज़रूरत के हिसाब से कितने भी parameters accept कर सकता है, अपनी definition और हर call site में order में और commas से अलग list किए गए — जो arguments आप pass करते हैं उन्हें parameter list के order और count से match करना चाहिए (या expected types में convertible होना चाहिए)।
उदाहरण: Multiple Parameters
// Include std::cout and std::cin
#include <iostream>
int addThree(int a, int b, int c) {
// Send a + b + c back to the caller
return a + b + c;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << addThree(1, 2, 3) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Array Parameters
Arrays default से C++ में reference से pass होते हैं, एक explicit & के बिना भी, जिसका मतलब है function द्वारा array elements में किए गए कोई भी changes caller में original array को affect करते हैं।
यह ordinary variables से अलग है और array parameters को value parameters जैसे behave करने की उम्मीद करने वाले beginners के लिए एक common surprise है।
उदाहरण: Array Parameters
// Include std::cout and std::cin
#include <iostream>
void doubleValues(int arr[], int size) {
// Loop: repeat while the condition holds
for (int i = 0; i < size; i++) {
arr[i] *= 2;
}
}
// Program execution starts in main()
int main() {
int nums[] = {1, 2, 3};
doubleValues(nums, 3);
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Print to the console with cout
std::cout << nums[i] << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि एक function caller का variable बदल देगा जब इसे value से एक copy मिली थी।
- एक array pass करना और parameter पर
sizeofइस्तेमाल करना, जो array की बजाय एक pointer का size देता है। - एक reference parameter पर
constभूल जाना, ताकि function accidentally argument बदल सके।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions