C++ में Multiple Function Parameters
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Position से Arguments Match करना
जब एक function call होता है, हर argument list में इसकी position से corresponding parameter से match होता है, नाम से नहीं, इसलिए arguments pass होने का order मायने रखता है।
उदाहरण: Matching Arguments by Position
// 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;
}
Login to try C/C++/Java/PHP code in the editor
अलग Types के Parameters
एक function के parameters सबका same type होना ज़रूरी नहीं; एक parameter list में int, double, string, और दूसरे types मिलाना common और पूरी तरह supported है।
उदाहरण: Parameters of Different Types
// 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;
}
Login to try C/C++/Java/PHP code in the editor
कई Parameters साथ इस्तेमाल करना
कई parameters अक्सर एक result compute करने के लिए function body के अंदर combine होते हैं, जैसे एक quantity को एक price से multiply करना या दो values को एक-दूसरे से compare करना।
उदाहरण: Using Multiple Parameters Together
// 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;
}
Login to try C/C++/Java/PHP code in the editor
बहुत ज़्यादा या बहुत कम Arguments
एक function को गलत संख्या में arguments से call करना -- बहुत कम या बहुत ज़्यादा -- C++ में एक compile-time error है, क्योंकि compiler check करता है कि हर call function की declared parameter list से match करे।
उदाहरण: Too Many or Too Few Arguments
#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;
}
Login to try C/C++/Java/PHP code in the editor
- arguments को गलत order में pass करना, जो compile होता है लेकिन types match होने पर गलत results देता है।
- बहुत कम या बहुत ज़्यादा arguments से call करना, जो एक compile error है।
int add(int a, b)लिखना और दूसरे parameter पर type छोड़ देना।
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