← Back to C Course | Chapter 5: Functions | Lesson 2 of 8

C Function के Parameters

Parameters एक function में खाली slots हैं जिन्हें आप हर बार भरते हैं, एक recipe में कितने eggs चाहिए इसके blanks की तरह। जो values आप pass करते हैं उन्हें arguments कहा जाता है।
Syntax
c
return_type function_name(type param1, type param2) {
  // body
}

function_name(argument1, argument2);

Parameters क्या हैं?

Parameters एक function की definition में listed variables हैं जो function call होने पर external data receive करते हैं, हर बार चलने पर same function body को अलग values पर operate करने देते हुए।

उदाहरण: What are Parameters?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void greet(char name) {
	// Print formatted text to the screen
	printf("Hello, %c", name);
}
// Program execution starts in main()
int main() {
	greet('J');
	// Return 0 to signal that the program finished successfully
	return 0;
}

Single Parameter

एक single parameter वाला function -- जैसे void greet(char name) -- caller को exactly एक argument pass करने की ज़रूरत करता है जिसका type उस parameter के declared type से match करे, वरना compiler एक mismatch flag करेगा।

उदाहरण: Single Parameter

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void greet(char name) {
	// Print formatted text to the screen
	printf("Hello, %c", name);
}
// Program execution starts in main()
int main() {
	greet('A');
	// Return 0 to signal that the program finished successfully
	return 0;
}

कई Parameters

कई parameters को commas से अलग करना, जैसे int add(int a, int b), एक function को एक साथ कई pieces of input accept करने देता है, सब function body के अंदर अपने local names के तहत available।

उदाहरण: Multiple Parameters

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", add(3, 4));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Parameter Types

C parameters किसी भी valid data type की तरह declared हो सकते हैं -- int, float, double, char, और आगे -- और आपके call पर दिए गए arguments को उन types और उनके order से मिलना चाहिए, क्योंकि C arguments को positionally match करता है, नाम से नहीं।

उदाहरण: Parameter Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
void showInfo(int age, float height, char grade) {
	// Print formatted text to the screen
	printf("%d %.1f %c", age, height, grade);
}
// Program execution starts in main()
int main() {
	showInfo(20, 5.9f, 'A');
	// Return 0 to signal that the program finished successfully
	return 0;
}

Actual बनाम Formal Arguments

एक function की अपनी definition में listed parameters formal arguments कहलाते हैं, जबकि इसे call करते समय आप जो actual values या variables pass करते हैं उन्हें actual arguments कहते हैं -- यह distinction तब मायने रखता है जब यह discuss किया जाए कि data एक function में कैसे flow करता है।

उदाहरण: Actual vs. Formal Arguments

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
int add(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
// Program execution starts in main()
int main() {
	int x = 3, y = 4;
	// Print formatted text to the screen
	printf("%d", add(x, y));
	// 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. arguments को गलत order में pass करना, जो compile होता है लेकिन types match होने पर गलत results देता है।
  2. यह उम्मीद करना कि एक function caller का variable बदल देगा जब उसे एक copy मिली थी।
  3. एक array pass करना और parameter पर sizeof इस्तेमाल करना, जो एक pointer का size देता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

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.