← Back to C++ Course | Chapter 5: Functions | Lesson 5 of 13

C++ में Functions को Structures Pass करना

एक struct को एक function को value से (एक copy की तरह), reference से (original modify करने के लिए), या const reference से (efficient read-only access के लिए) pass किया जा सकता है, हर एक एक अलग use case के लिए suited है।
Syntax
cpp
struct StructName {
  type member;
};

void function_name(StructName s);   // by value (copy)
void function_name(StructName &s);  // by reference

Value से एक Struct Pass करना

एक struct को value से pass करना function को struct की अपनी पूरी copy देता है, इसलिए function के अंदर किए गए कोई भी changes caller के original struct पर कोई असर नहीं डालते।

उदाहरण: Passing a Struct by Value

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

void tryModify(Point p) {
	p.x = 100;
}

// Program execution starts in main()
int main() {
	Point pt = {1, 2};
	tryModify(pt);
	// Print to the console with cout
	std::cout << pt.x << "," << pt.y << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Reference से एक Struct Pass करना

&, इस्तेमाल करके reference से एक struct pass करना function को caller का original struct सीधे modify करने देता है, क्योंकि parameter एक copy की बजाय same memory refer करता है।

उदाहरण: Passing a Struct by Reference

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

void modify(Point &p) {
	p.x = 100;
}

// Program execution starts in main()
int main() {
	Point pt = {1, 2};
	modify(pt);
	// Print to the console with cout
	std::cout << pt.x << "," << pt.y << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

const Reference से एक Struct Pass करना

const reference से एक struct pass करना potentially बड़े struct को copy करने की cost avoid करता है साथ ही function को caller का original data modify करने से भी रोकते हुए, efficiency को safety के साथ combine करते हुए।

उदाहरण: Passing a Struct by const Reference

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

void printPoint(const Point &p) {
	// Print to the console with cout
	std::cout << p.x << "," << p.y << std::endl;
}

// Program execution starts in main()
int main() {
	Point pt = {5, 7};
	printPoint(pt);
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Function से एक Struct Return करना

एक function value से एक struct भी return कर सकता है, caller को एक पूरा struct construct करके वापस देते हुए, जो कई related results को साथ bundle करने का एक common तरीका है।

उदाहरण: Returning a Struct from a Function

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

Point makeOrigin() {
	Point p;
	p.x = 0;
	p.y = 0;
	// Send p back to the caller
	return p;
}

// Program execution starts in main()
int main() {
	Point origin = makeOrigin();
	// Print to the console with cout
	std::cout << origin.x << "," << origin.y << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Structs का एक Array Pass करना

Structs का एक array function को pass होने पर एक pointer में decay होता है, किसी भी दूसरे type के array की तरह, इसलिए function को आमतौर पर array की length भी एक अलग parameter की तरह मिलती है।

उदाहरण: Passing an Array of Structs

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

void printAll(Point points[], int size) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < size; i++) {
		// Print to the console with cout
		std::cout << points[i].x << "," << points[i].y << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
}

// Program execution starts in main()
int main() {
	Point pts[] = {{1, 2}, {3, 4}};
	printAll(pts, 2);
	// Return 0 to signal that the program finished successfully
	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. हर बार एक बड़ा struct value से pass करना, जो सब कुछ copy करता है और program धीमा करता है।
  2. value से pass किए एक struct को modify करना और original बदलने की उम्मीद करना।
  3. जब function को struct सिर्फ पढ़ना चाहिए तो const भूल जाना।

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.