Question

In: Computer Science

Explain Parameter passing Semantics in Java/C++

Explain Parameter passing Semantics in Java/C++

Solutions

Expert Solution

There are two type or parameter passing semantics in java/c++

1. Pass by value

2. Pass by reference

1) Pass by value :- In the pass by value the original data are not modified, it will just copy the data which we will pass.

For example:-

#include <iostream>
using namespace std;
void pass(int value) {
cout << "Value passed by parameter into function: " << value << endl;
value = 20;
cout << "Value before function end: " << value << endl;
}
int main() {
int value = 10;
cout << "Value before calling : " << value << endl;
pass(value);
cout << "Value after calling: " << value << endl;
return 0;
}

/******************output**********************/

Value before calling : 10
Value passed by parameter into function: 10
Value before function end: 20
Value after calling: 10

--------------------------------

2) Pass by reference :- It will change the original value of the variable.

For Example:
#include <iostream>
using namespace std;
void process(int& value) {
cout << "Value passed by parameter into function: " << value << endl;
value = 20;
cout << "Value before function end: " << value << endl;
}
int main() {
int value = 10;
cout << "Value before calling : " << value << endl;
process(value);
cout << "Value after calling: " << value << endl;
return 0;
}

/****************output************************/

Value before calling : 10
Value passed by parameter into function: 10
Value before function end: 20
Value after calling: 20

--------------------------------

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Explain using the operational semantics the meaning of the for syntax in C (3 marks): for...
Explain using the operational semantics the meaning of the for syntax in C : for (expr1; expr2; expr3) { . . . } (C for) for (expr1; expr2; expr3) ... evaluate(expr1) loop: control = evaluate(expr2) if control == 0 goto out ... evaluate(expr3) goto loop out: ...
Explain what denotational semantics are with an example. Provide the denotational semantics for x = x...
Explain what denotational semantics are with an example. Provide the denotational semantics for x = x + 1 along with the justification.
Which of the following is a motivation for passing functions around in Java? Select one: a....
Which of the following is a motivation for passing functions around in Java? Select one: a. Compiled code with anonymous functions is smaller in size than with explicit functions. (INCORRECT)b. Passing functions as objects around and applying them is significantly less complex than writing the logic for those algorithms directly. c. It is the only way for the class to allow clients to access data that is otherwise private. d. It allows clients to provide an algorithm without ownership of...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Solve this problem using pointer variables for parameter passing, where appropriate and pointer arithmetic when working...
Solve this problem using pointer variables for parameter passing, where appropriate and pointer arithmetic when working with arrays Please use the "C" program toProduce the EXACT output shown at the end of the document. 1.Generate a graph that compares, on a month-by-month basis, the monthly rainfall for Kamloops for the first half of 2018 (i.e. Jan – June) versus the normal (30 year average) rainfall for Kamloops for the same months. In main( ), create the 2 data arrays using...
Explain HTML Semantics with example Programs(atleast 5-10 programs with clear explanation )
Explain HTML Semantics with example Programs(atleast 5-10 programs with clear explanation )
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a denotational semantics for do-while loop
Write a denotational semantics for do-while loop
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT