Functions
Understanding Functions in C Programming
Introduction to Functions (फंक्शन का परिचय)
Functions code के reusable blocks होते हैं जो specific task perform करते हैं। ये program को modular और maintainable बनाते हैं।
Functions are reusable blocks of code that perform specific tasks. They make programs modular and maintainable.
Key Points (मुख्य बिंदु):
- Functions code को organize और reuse करने में help करते हैं
- Functions help in organizing and reusing code
- प्रत्येक function का एक unique name और return type होता है
- Each function has a unique name and return type
- Functions parameters accept कर सकते हैं और values return कर सकते हैं
- Functions can accept parameters and return values
Function Declaration and Definition (फंक्शन डिक्लेरेशन और डेफिनिशन)
Function declaration और definition function के structure को define करते हैं:
Function declaration and definition define the structure of a function:
// Function declaration (prototype)
return_type function_name(parameter_list);
// Function definition
return_type function_name(parameter_list) {
// Function body
// Statements
return value; // Optional
}
Component (घटक) | Description (विवरण) | Example (उदाहरण) |
---|---|---|
return_type | Function द्वारा return की जाने वाली value का type | int, float, void |
return_type | Type of value returned by the function | int, float, void |
function_name | Function का unique identifier | add, calculate, print |
function_name | Unique identifier for the function | add, calculate, print |
parameter_list | Function को pass किए जाने वाले parameters | (int a, int b) |
parameter_list | Parameters passed to the function | (int a, int b) |
Types of Functions (फंक्शन के प्रकार)
C में विभिन्न प्रकार के functions होते हैं:
C has different types of functions:
// Function with no parameters and no return value
void greet() {
printf("Hello!\n");
}
// Function with parameters and return value
int add(int a, int b) {
return a + b;
}
// Function with parameters but no return value
void printSum(int a, int b) {
printf("Sum: %d\n", a + b);
}
// Recursive function
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
Function Call and Parameters (फंक्शन कॉल और पैरामीटर्स)
Function call और parameters function के execution को control करते हैं:
Function call and parameters control function execution:
// Function call examples
int result;
// Call function with no parameters
greet();
// Call function with parameters
result = add(5, 3);
printf("Sum: %d\n", result);
// Call function with parameters but no return value
printSum(10, 20);
// Call recursive function
int fact = factorial(5);
printf("Factorial: %d\n", fact);
Best Practices (सर्वोत्तम प्रथाएं)
Functions के साथ काम करने के best practices:
Best practices for working with functions: