In: Computer Science
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)
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 :
Step 2
In C++,
Call by value
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
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
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
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.