C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Difference Between Formal Parameters And Actual Parameters In C Programming Language

In C programming language, when working with functions, you will encounter two types of parameters: formal parameters and actual parameters. These terms are used to differentiate between the parameters used in the function definition (formal parameters) and the arguments passed to the function when it is called (actual parameters). Here's the difference between the two:

Formal parameters:

Formal parameters are the variables that are defined in the function signature. These are also known as "dummy" parameters or simply "parameters". They are placeholders for the actual values that will be passed to the function when it is called. Formal parameters are used in the body of the function to perform the desired operations.

Example of formal parameters in a function definition:

int add(int a, int b) {
  return a + b;
}

In this example, int a and int b are formal parameters of the add function.

Actual parameters:

Actual parameters, also known as "arguments", are the values that are passed to the function when it is called. These values are used to initialize the formal parameters of the function. Actual parameters can be constants, variables, or expressions, and their types should match the types of the corresponding formal parameters.

Example of actual parameters in a function call:

int main() {
  int x = 5;
  int y = 7;
  int sum = add(x, y);
  printf("The sum is: %d\n", sum);
  return 0;
}

In this example, the variables x and y are actual parameters, passed to the add function when it is called.

Summary:

  • Formal parameters are used in the function definition to define the expected input data types for the function.
  • Actual parameters are the values or variables passed to the function when it is called.
  • Formal parameters act as placeholders for the actual parameters in the function body.
  • The types of the actual parameters should match the types of the corresponding formal parameters.

Understanding the difference between formal parameters and actual parameters in C programming language is essential for working with functions, as it helps you correctly define functions and pass appropriate arguments when calling them.

  1. Formal Parameters Definition and Usage in C:

    Formal parameters are placeholders in a function's definition. They are used to receive values when the function is called.

    #include <stdio.h>
    
    void greet(char name[]) {
        printf("Hello, %s!\n", name);
    }
    
    int main() {
        char person[] = "Alice";
        greet(person); // "person" is the actual parameter
        return 0;
    }
    

    Output:

    Hello, Alice!
    
  2. Actual Parameters and Arguments in C Programming:

    Actual parameters are the values passed to a function when it is called. They correspond to formal parameters in the function definition.

    #include <stdio.h>
    
    void addNumbers(int a, int b) {
        printf("Sum: %d\n", a + b);
    }
    
    int main() {
        addNumbers(5, 7); // 5 and 7 are actual parameters
        return 0;
    }
    

    Output:

    Sum: 12
    
  3. Passing Values to Functions in C:

    Values can be passed to functions through actual parameters, allowing flexibility in function behavior.

    #include <stdio.h>
    
    void multiply(int x, int y) {
        printf("Product: %d\n", x * y);
    }
    
    int main() {
        int num1 = 3, num2 = 4;
        multiply(num1, num2); // num1 and num2 are passed as values
        return 0;
    }
    

    Output:

    Product: 12
    
  4. C Code Examples Illustrating Formal and Actual Parameters:

    #include <stdio.h>
    
    void printNumbers(int start, int end) {
        for (int i = start; i <= end; ++i) {
            printf("%d ", i);
        }
        printf("\n");
    }
    
    int main() {
        printNumbers(1, 5); // 1 and 5 are actual parameters
        return 0;
    }
    

    Output:

    1 2 3 4 5
    

    Demonstrates a function with formal parameters start and end.

  5. Default Values and Optional Parameters in C Functions:

    C does not natively support default values or optional parameters, but you can achieve similar behavior using function overloading or varargs.

    #include <stdio.h>
    
    void greetUser(char name[]) {
        printf("Hello, %s!\n", name);
    }
    
    void greetGuest() {
        printf("Hello, Guest!\n");
    }
    
    int main() {
        greetUser("Alice");
        greetGuest(); // No arguments, providing a default behavior
        return 0;
    }
    

    Output:

    Hello, Alice!
    Hello, Guest!
    

    Demonstrates a function with optional parameters.

  6. Handling Variable Number of Arguments in C:

    Variable arguments can be handled using stdarg.h functions like va_start and va_arg.

    #include <stdio.h>
    #include <stdarg.h>
    
    int sum(int num, ...) {
        va_list args;
        va_start(args, num);
    
        int result = 0;
        for (int i = 0; i < num; ++i) {
            result += va_arg(args, int);
        }
    
        va_end(args);
        return result;
    }
    
    int main() {
        int result = sum(3, 1, 2, 3); // Variable number of arguments
        printf("Sum: %d\n", result);
        return 0;
    }
    

    Output:

    Sum: 6
    
  7. Avoiding Common Mistakes with Formal and Actual Parameters in C:

    • Ensure that the number and types of actual parameters match the formal parameters in the function definition.
    • Be mindful of parameter order to avoid unexpected behavior.
    #include <stdio.h>
    
    void printInfo(int age, char gender) {
        printf("Age: %d, Gender: %c\n", age, gender);
    }
    
    int main() {
        printInfo('M', 25); // Mistakenly swapping the order of arguments
        return 0;
    }
    

    Output:

    Age: 77, Gender: M
    

    Incorrect parameter order can lead to unexpected results.