Question

In: Computer Science

Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages...

Some languages support many modes of parameter passing. Provide 2 examples using two different programming languages which support the user of the programming language deciding which to use when creating their method.

(Programming Languages)

Solutions

Expert Solution

Answer.

Step 1

There are different ways in which parameter data can be passed into and out of methods or functions. Let us assume that a function B() is called from another function A(). In this case A is called the “caller function” and B is called the “called function”. Also, the arguments which A sends to B are called actual arguments and the parameters of B are called formal arguments.

Important methods of parameter passing :

  1. Call by Value.
  2. Call by reference.

Step 2

In C++,

Call by value

  • In call by value, value of variable is passed during function call.
  • And copy this value in another variable at function definition.
  • In call by value the original value in calling function will never change after execution of function.

example.

#include<iostream>

using namespace std;

void swap (int a, int b) {

int temp=a;

a=b;

b=temp;

}

int main () {

int x, y;

cout<<"enter value of a and b:";

cin>>x>>y;

swap (x, y);

cout<<"\nafter swapping a = "<<a<<"and b = "<<b;

return 0;

}

Output:

Enter value of a and b:

4

5

After swapping a = 5 and b = 4

Call by reference

  • In call by reference, reference is passed during function call.
  • The formal arguments in the called function become aliases to the actual function call.
  • In call by reference the original value in calling function will change after execution of function.

example.

#include<iostream>

using namespace std;

void swap (int &a, int &b) // It is the only difference from above program

{

int temp=a;

a=b;

b=temp;

}

int main () {

int a,b;

cout<<"Enter two numbers:";

cin>>a>>b;

swap (a, b);

cout<<"a="<<a<<"b="<<b;

return 0;

}

Output:

Enter value of a and b:

4

5

After swapping a = 5 and b = 4

Step 3

In JAVA,

Call by value

  • Call by Value means calling a method with a parameter as value.
  • Through this, the argument value is passed to the parameter.

example.

public class TestByValue{
   public static void main(String[] args){
      int a = 30;
      int b = 45;
      System.out.println("Before swapping, a = " + a + " and b = " + b);
      // Invoke the swap method
      swap(a, b);
      System.out.println("\n**Now, Before and After swapping values will be same here**:");
      System.out.println("After swapping, a = " + a + " and b is " + b);
   }
   public static void swap(int a, int b) {
      System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
      // Swap n1 with n2
      int c = a;
      a = b;
      b = c;
      System.out.println("After swapping(Inside), a = " + a + " b = " + b);
   }
}

Output:

Before swapping, a = 10 and b = 15
Before swapping(Inside), a = 10 b = 15
After swapping(Inside), a = 15 b = 10
**Now, Before and After swapping values will be same here**:
After swapping, a = 10 and b is 15

Call by reference

  • Call by Reference means calling a method with a parameter as a reference.
  • Through this, the argument reference is passed to the parameter.

example.

public class TestByReference {
   public static void main(String[] args) {
      IntWrapper a = new IntWrapper(30);
      IntWrapper b = new IntWrapper(45);
      System.out.println("Before swapping, a = " + a.a + " and b = " + b.a);
      // Invoke the swap method
      swap(a, b);
      System.out.println("\n**Now, Before and After swapping values will be different here**:");
      System.out.println("After swapping, a = " + a.a + " and b is " + b.a);
   }
   public static void swap(IntWrapper a, IntWrapper b) {
      System.out.println("Before swapping(Inside), a = " + a.a + " b = " + b.a);
      // Swap n1 with n2
      IntWrapper c = new IntWrapper(a.a);
      a.a = b.a;
      b.a = c.a;
      System.out.println("After swapping(Inside), a = " + a.a + " b = " + b.a);
   }
}
class IntWrapper {
   public int a;
   public IntWrapper(int a){ this.a = a;}
}

Output:

Before swapping, a = 10 and b = 15
Before swapping(Inside), a = 10 b = 15
After swapping(Inside), a = 15 b = 10
**Now, Before and After swapping values will be different here**:
After swapping, a = 15 and b is 10

Please upvote me.

Thank you.


Related Solutions

(a) Explain at least TWO (2) major aspects of an object-oriented programming (OOP). Provide suitable examples,
(a) Explain at least TWO (2) major aspects of an object-oriented programming (OOP). Provide suitable examples,
A) Provide two examples of a Support Worker’s obligation in regards to duty of care in...
A) Provide two examples of a Support Worker’s obligation in regards to duty of care in relation to Stephanie’s behaviour with food. 1. 2. B) Provide two examples of Stephanie’s dignity of risk in relation to her behaviour with food. 1. 2. (Below I have included Stephanie's care plan) Stephanie Peterson is 45 years old and lives at Harbourside Haven, a specialist disability unit that is close to the ocean. She has been there only four months after living in...
Provide two examples of False confessions that support your opinion within this topic
Provide two examples of False confessions that support your opinion within this topic
Provide some examples of how your company uses these types of metrics to support decision making...
Provide some examples of how your company uses these types of metrics to support decision making or how they should be using it. a) The use of categorical data vs quantitative data b) The diffrence between a frequency table and a relative frequency table. c) The diffrence between a bar chart and a histogram d) Discuss the application of simpsions paradox to unaggregated data and its impact on drawing conclusions from aggregated data.
Provide some examples of different types of waste in an organization with which you are familiar....
Provide some examples of different types of waste in an organization with which you are familiar. How do you minimize or eliminate it?
(2) What are your beliefs about the causes of health and disease? Provide support and examples...
(2) What are your beliefs about the causes of health and disease? Provide support and examples to defend your position. The support and examples should come from readings, videos, and personal observations. reading: The social determinants of health (SDH) are the conditions in which people are born, grow, work, live, and age, and the wider set of forces and systems shaping the conditions of daily life. These forces and systems include economic policies and systems, development agendas, social norms, social...
Provide two different examples of how research uses hypothesis testing.
Provide two different examples of how research uses hypothesis testing.
describe the difference between organic and paid search and provide some examples of the different tools...
describe the difference between organic and paid search and provide some examples of the different tools and tactics you would use to optimize each channel thoroughout a user journey?
1. Which Interrupt Has The Highest Priority? 2. Name 5 Different Addressing Modes? 3. How Many...
1. Which Interrupt Has The Highest Priority? 2. Name 5 Different Addressing Modes? 3. How Many Interrupts Are There In 8085? 4. In 8085 Which Is Called As High Order / Low Order Register? 5. What Are Input & Output Devices?
Provide 2 (two) examples for the following in relation to the palliative care.
Provide 2 (two) examples for the following in relation to the palliative care.Terms10.1)     Policy10.2)     Protocol10.3)     Procedure
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT