C++ में Arrays और Functions
In this page:
return_type function_name(data_type array_param[], int size) {
// body
}
function_name(array_name, size);
Functions को Arrays Pass करना
एक array को एक function को pass करना argument की तरह brackets के बिना array का नाम इस्तेमाल करता है, जबकि receiving parameter empty brackets के साथ लिखा जाता है, जैसे void process(int arr[])।
अंदर से, actually जो pass होता है वह array का starting memory address है, इसके contents की एक पूरी copy नहीं।
उदाहरण: Passing Arrays to Functions
// Include std::cout and std::cin
#include <iostream>
void printArr(int arr[], int size) {
// Loop: repeat while the condition holds
for (int i = 0; i < size; i++) {
// Print to the console with cout
std::cout << arr[i] << " ";
}
// Print to the console with cout
std::cout << std::endl;
}
// Program execution starts in main()
int main() {
int nums[] = {1, 2, 3};
printArr(nums, 3);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Array Size Pass करना
क्योंकि एक array parameter असल में सिर्फ इसके पहले element का एक pointer है, function के पास यह जानने का कोई तरीका नहीं कि original array actually कितने elements रखता है — आपको size को एक अलग integer parameter की तरह explicitly pass करना होगा, जैसे void process(int arr[], int size), वरना function array की real bounds से आगे पढ़ने का risk लेता है।
उदाहरण: Passing Array Size
#include <iostream>
void printArr(int arr[], int size) {
for (int i = 0; i < size; i++) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
int main() {
int nums[] = {4, 5, 6, 7};
printArr(nums, 4); // size must be passed separately
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Functions में Arrays Modify करना
क्योंकि arrays effectively reference से pass होते हैं (उनके memory address के जरिए), function द्वारा array के elements में किया गया कोई भी modification सीधे caller में original array बदलता है — ordinary variables के उलट, कोई automatic protective copy नहीं बनती।
उदाहरण: Modifying Arrays in Functions
// Include std::cout and std::cin
#include <iostream>
void doubleAll(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};
doubleAll(nums, 3);
// Print to the console with cout
std::cout << nums[0] << " " << nums[1] << " " << nums[2] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multi-dimensional Arrays Pass करना
एक 2D array को एक function को pass करते समय, first dimension का size parameter से omit किया जा सकता है, जैसे void process(int grid[][4]), लेकिन second (column) dimension को हमेशा exactly specify करना ज़रूरी है, क्योंकि compiler को हर row के लिए memory offset calculate करने के लिए इसकी ज़रूरत है।
उदाहरण: Passing Multi-dimensional Arrays
// Include std::cout and std::cin
#include <iostream>
void process(int grid[][4], int rows) {
// Loop: repeat while the condition holds
for (int i = 0; i < rows; i++) {
// Loop: repeat while the condition holds
for (int j = 0; j < 4; j++) {
// Print to the console with cout
std::cout << grid[i][j] << " ";
}
}
// Print to the console with cout
std::cout << std::endl;
}
// Program execution starts in main()
int main() {
int grid[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
process(grid, 2);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Const Array Parameters
एक array parameter में const add करना, जैसे void print(const int arr[], int size), signal करता है कि function सिर्फ array के contents पढ़ेगा और इन्हें modify न करने का वादा करता है — compiler फिर function के अंदर किसी भी accidental write को एक error की तरह flag करेगा, program actually चलने से पहले ही bugs की एक class पकड़ते हुए।
उदाहरण: Const Array Parameters
#include <iostream>
void print(const int arr[], int size) {
for (int i = 0; i < size; i++) {
std::cout << arr[i] << " ";
// arr[i] = 0; // compile error: cannot modify a const array parameter
}
std::cout << std::endl;
}
int main() {
int nums[] = {1, 2, 3};
print(nums, 3);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- function के अंदर
sizeofसे array की length मिलने की उम्मीद करना। - array size pass करना भूल जाना, ताकि function अंत से आगे पढ़े।
- यह मान लेना कि arrays pass होने पर copy होते हैं, जब function actually original पर काम करता है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings