Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

Argument/Parameter:

Argument is used to  pass information between Functions and Main Program. These are also called as Parameters. Argument can be integer variable, float variable, Arguments can be any in Number.

e.g.   method of defining arguments.

         return type function-name (list of arguments);

         void add(int a1, int b1)

Here a1, b1 are arguments add is name of Function void is return type of Function.

Type of Argument/Parameter:

1. Formal Argument:

  • Definition: Formal arguments are the parameters declared in the function signature or definition.
  • Location: They are part of the function’s definition, enclosed in parentheses following the function name.
  • Purpose: Formal arguments act as placeholders for the values that will be passed to the function when it is called.
  • Example:

// Formal arguments in the function definition
int multiply(int x, int y) {
return x + y;
}

In this example, x and y are formal arguments.

2. Actual Argument:

  • Definition: Actual arguments are the values or expressions passed to the function during its invocation (function call).
  • Location: They appear inside the parentheses during the function call.
  • Purpose: Actual arguments provide the concrete values that will be used for the corresponding formal arguments in the function.
  • Example:

// Function call with actual arguments
int result = multiply(3, 5);

In this example, 3 and 5 are actual arguments passed to the multiply function.


Argument/Parameter Passing: 

Arguments can passing in two ways:

1. Call By Value:

The values of the actual arguments are copied to the formal arguments. Any changes made to the formal arguments inside the function do not affect the actual arguments.

Example:

#include <stdio.h>

// Function prototype for call by value
void swapByValue(int a, int b);

int main() {
    int num1 = 10;
    int num2 = 20;

    // Display the values before the swap
    printf(“Before swap:n”);
    printf(“num1: %d, num2: %dn”, num1, num2);

    // Call the function with call by value to swap the values
    swapByValue(num1, num2);

    // Display the values after the swap
    printf(“After swap (call by value does not modify original values):n”);
    printf(“num1: %d, num2: %dn”, num1, num2);

    return 0;
}

Output:

Before swap:
num1: 10, num2: 20
After swap (call by value does not modify original values):
num1: 10, num2: 20

2. Call By Reference:

The addresses (references) of the actual arguments are passed to the formal arguments. Changes made to the values at those addresses inside the function affect the actual arguments.

Example:

#include <stdio.h>

// Function prototype for call by reference
void swapByReference(int *a, int *b);

int main() {
    int num1 = 10;
    int num2 = 20;

    // Display the values before the swap
    printf(“Before swap:n”);
    printf(“num1: %d, num2: %dn”, num1, num2);

    // Call the function with call by reference to swap the values
    swapByReference(&num1, &num2);

    // Display the values after the swap
    printf(“After swap (call by reference modifies original values):n”);
    printf(“num1: %d, num2: %dn”, num1, num2);

    return 0;
}

// Function definition for call by reference
void swapByReference(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
    // Changes inside the function directly affect the original values in main
}

Output:

Before swap:
num1: 10, num2: 20
After swap (call by reference modifies original values):
num1: 20, num2: 10


Difference Between Call By Value & Call By Reference: