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);
}
  • Library Functions: C standard library में predefined functions
  • Library Functions: Predefined functions in C standard library
  • User-defined Functions: Programmer द्वारा created functions
  • User-defined Functions: Functions created by the programmer
  • Recursive Functions: खुद को call करने वाले functions
  • Recursive Functions: Functions that call themselves
  • 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);
  • Actual Parameters: Function call में pass किए जाने वाले values
  • Actual Parameters: Values passed in function call
  • Formal Parameters: Function definition में declared variables
  • Formal Parameters: Variables declared in function definition
  • Pass by Value: C में parameters by value pass होते हैं
  • Pass by Value: Parameters are passed by value in C
  • Best Practices (सर्वोत्तम प्रथाएं)

    Functions के साथ काम करने के best practices:

    Best practices for working with functions:

  • Single Responsibility: प्रत्येक function एक specific task perform करे
  • Single Responsibility: Each function should perform one specific task
  • Meaningful Names: Functions को descriptive names दें
  • Meaningful Names: Give functions descriptive names
  • Documentation: Functions को properly document करें
  • Documentation: Properly document your functions
  • Parameter Validation: Input parameters को validate करें
  • Parameter Validation: Validate input parameters