Explain the parameter passing models briefly.


Q.) Explain the parameter passing models briefly.

Subject: Object Oriented Programming Methodology

Parameter Passing Models:

In computer programming, parameter passing models specify how arguments are passed from the caller to the callee function. The choice of parameter passing model can affect the efficiency, security, and ease of programming of a program.

There are various parameter passing models, each with its own advantages and disadvantages. Here are some of the commonly used parameter passing models:

1. Pass by Value:

In pass by value, a copy of the actual argument is created and passed to the function. This means that any changes made to the formal parameter inside the function do not affect the actual argument in the caller. This model is simple to understand, and it ensures that the actual argument remains unchanged within the function. However, it can be inefficient for large objects, as the copy of the argument takes additional memory and time to create.

Example in C++:

void swap(int x, int y) {
  int temp = x;
  x = y;
  y = temp;
}

In this example, x and y are passed by value to the swap function. Any changes made to x and y within the function do not affect the actual arguments in the caller.

2. Pass by Reference:

In pass by reference, the address of the actual argument is passed to the function. This means that any changes made to the formal parameter inside the function are reflected in the actual argument in the caller. This model is more efficient for large objects, as it avoids the need to copy the object. However, it requires careful programming to avoid accidental modifications of the actual argument.

Example in C++:

void swap(int &x, int &y) {
  int temp = x;
  x = y;
  y = temp;
}

In this example, x and y are passed by reference to the swap function. Changes made to x and y are directly reflected in the actual arguments in the caller.

3. Pass by Value-Result:

In pass by value-result, a copy of the actual argument is created and passed to the function, and the modified value is returned to the caller through the function return value. This model is a combination of pass by value and pass by reference. It allows the function to modify the argument, but the changes are only reflected in the caller if the function return value is assigned to the actual argument.

Example in Python:

def swap(x, y):
  temp = x
  x = y
  y = temp
  return x, y

a, b = swap(a, b)

In this example, a and b are passed by value-result to the swap function. The changes made to x and y are only reflected in a and b if the function return value is assigned to them.

4. Pass by Copy-In Copy-Out:

In pass by copy-in copy-out, a copy of the actual argument is created and passed to the function, and a copy of the modified value is returned to the caller. This model ensures that both the actual argument and the modified value are kept separate, allowing the function to modify the argument without affecting the caller.

Example in Java:

public class Swap {
  public static void swap(int[] arr) {
    int temp = arr[0];
    arr[0] = arr[1];
    arr[1] = temp;
  }

  public static void main(String[] args) {
    int[] arr = {1, 2};
    swap(arr);
    System.out.println(arr[0] + " " + arr[1]);
  }
}

In this example, the swap function takes an array as an argument and swaps the first two elements. However, since the array is passed by copy-in copy-out, the actual array in the caller remains unchanged.

The choice of parameter passing model depends on the specific requirements of the programming language, the program's performance, and the programmer's preferences.