← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 6 of 15

C++ में Arrays और Functions

एक array को एक function को pass करना boxes की original row सौंप देता है, ताकि function उन्हें पढ़ या बदल सके। आप आमतौर पर size भी pass करते हैं, क्योंकि function इसे नहीं जानता।
Syntax
cpp
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

cpp
// 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;
}

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

cpp
#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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
#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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. function के अंदर sizeof से array की length मिलने की उम्मीद करना।
  2. array size pass करना भूल जाना, ताकि function अंत से आगे पढ़े।
  3. यह मान लेना कि arrays pass होने पर copy होते हैं, जब function actually original पर काम करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.