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

C++ में Multiple Function Parameters

एक function commas से अलग किए कई parameters accept कर सकता है, हर एक का अपना type, और function call होने पर arguments position से parameters से match होते हैं।
Syntax
cpp
return_type function_name(type param1, type param2) {
  // body
}

function_name(argument1, argument2);

कई Parameters Declare करना

कई parameters एक function के parentheses के अंदर declared होते हैं, commas से अलग किए गए, हर एक का अपना type और name, एक single function को एक साथ कई pieces of input accept करने देते हुए।

उदाहरण: Declaring Multiple Parameters

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

void showInfo(std::string name, int age) {
	// Print to the console with cout
	std::cout << name << " is " << age << std::endl;
}

// Program execution starts in main()
int main() {
	showInfo("Alex", 30);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Position से Arguments Match करना

जब एक function call होता है, हर argument list में इसकी position से corresponding parameter से match होता है, नाम से नहीं, इसलिए arguments pass होने का order मायने रखता है।

उदाहरण: Matching Arguments by Position

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

void divide(int a, int b) {
	// Print to the console with cout
	std::cout << a << " / " << b << " = " << a / b << std::endl;
}

// Program execution starts in main()
int main() {
	divide(10, 2);
	// Return 0 to signal that the program finished successfully
	return 0;
}

अलग Types के Parameters

एक function के parameters सबका same type होना ज़रूरी नहीं; एक parameter list में int, double, string, और दूसरे types मिलाना common और पूरी तरह supported है।

उदाहरण: Parameters of Different Types

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

void printOrder(std::string item, double price, int quantity) {
	// Print to the console with cout
	std::cout << quantity << "x " << item << " at $" << price << std::endl;
}

// Program execution starts in main()
int main() {
	printOrder("Book", 12.5, 3);
	// Return 0 to signal that the program finished successfully
	return 0;
}

कई Parameters साथ इस्तेमाल करना

कई parameters अक्सर एक result compute करने के लिए function body के अंदर combine होते हैं, जैसे एक quantity को एक price से multiply करना या दो values को एक-दूसरे से compare करना।

उदाहरण: Using Multiple Parameters Together

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

double totalCost(double price, int quantity) {
	// Send price * quantity back to the caller
	return price * quantity;
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << totalCost(9.99, 3) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

बहुत ज़्यादा या बहुत कम Arguments

एक function को गलत संख्या में arguments से call करना -- बहुत कम या बहुत ज़्यादा -- C++ में एक compile-time error है, क्योंकि compiler check करता है कि हर call function की declared parameter list से match करे।

उदाहरण: Too Many or Too Few Arguments

cpp
#include <iostream>

void add(int a, int b) {
	std::cout << a + b << std::endl;
}

int main() {
	add(2, 3);
	// add(2);       // compile-time error: too few arguments
	// add(2, 3, 4); // compile-time error: too many arguments
	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. arguments को गलत order में pass करना, जो compile होता है लेकिन types match होने पर गलत results देता है।
  2. बहुत कम या बहुत ज़्यादा arguments से call करना, जो एक compile error है।
  3. int add(int a, b) लिखना और दूसरे parameter पर type छोड़ देना।

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.