Question

In: Computer Science

Give one example that will show that pass by value, pass by Reference, pass by name,...

Give one example that will show that pass by value, pass by Reference, pass by name, pass by value result are all different.

Solutions

Expert Solution

Examples:

pass by value:

void incrementCount(int count)//pass by value

{

  count=count+1;//increments the value of count inside the function

}

int main()

{

  int count=0;// initialze the variable count

  int result=0;//  initialze the variable result

  incrementCount(count);//call increment function

  cout<<"Pass by value\n";

  cout<<"Count:";

  cout<<count;//prints the value of count after the function call

  return 0;

}

pass by reference

void incrementCount(int & count)//& to pass by reference

{

  count=count+1;//increments the value of count

}

int main()

{

  int count=0;//initialize the variable count

  int result=0;// initialize the variable result

  incrementCount(count);//increment value of count

  cout<<"Pass by Reference\n";

  cout<<"Count:";

  cout<<count;//prints count after the function call

  return 0;

}

pass by name:

real procedure Sum(j, lo, hi, Ej);
  value lo, hi;
  integer j, lo, hi; real Ej;
begin
  real S;
  S := 0;
  for j := lo step 1 until hi do
    S := S + Ej;
  Sum := S
end;

pass by value result:

#include <iostream>
#include <string.h>
using namespace std;

void swap(int a, int b)
{

  int temp;
    temp = a;
    a = b;
    b = temp;
}

int main()
{
  int value = 2;
  int  list[5] = {1, 3, 5, 7, 9};


  swap(value, list[0]);

  cout << value << "   " << list[0] << endl;

  swap(list[0], list[1]);

  cout << list[0] << "   " << list[1] << endl;

  swap(value, list[value]);

  cout << value << "   " << list[value] << endl;

}

Related Solutions

What is pass-by-reference? What is pass-by-value? What is a memory leak? What happens if the function...
What is pass-by-reference? What is pass-by-value? What is a memory leak? What happens if the function makes a change to that received array and why? Functions in C++ can return only one data item (e.g., variable) using the return statement and they also cannot return arrays. There is, however, another way to "return" more than one item from a function without using the return statement. Can you explain how and why? How are arrays represented in memory? What is the...
Name and describe the 7 layout strategies and give an example of each one
Name and describe the 7 layout strategies and give an example of each one
The girl's name is a reference to what? Choose one
The girl's name is a reference to what? Choose one Her figurative (and perhaps even literal) near-sightedness. The town in which she lives Her Slavic ancestry The kind of work she will do for a living
Name four organelles of the cell and give their basic structure and function. give reference
Name four organelles of the cell and give their basic structure and function. give reference
- Show an example of an observational study that looks or sounds scientific, with the reference....
- Show an example of an observational study that looks or sounds scientific, with the reference. 1 - Explain what makes the study look or seem scientific. 2 - Explain what the problems with the study are and some of the possible ramifications if you were to accept the claim(s) being made as true. 3 - Briefly discuss what might happen if many people accepted the claim(s) being made as true.
Explain situational leadership in reference to training and development. Give an example.
Explain situational leadership in reference to training and development. Give an example.
Give one example of how in business that the time value of money concepts will be...
Give one example of how in business that the time value of money concepts will be important. Give one examples for your personal life. In your examples use dollar estimates to show the impact
Please give one example for each level of the fair value hierarchy.
Please give one example for each level of the fair value hierarchy.
Give the name of cranial nerve X and one of its functions. Give the name and...
Give the name of cranial nerve X and one of its functions. Give the name and number of a cranial nerve - other than I, II, VIII, or X - and state one of its functions. Describe in detail the process of synaptic transmission. Start with the arrival of an action potential at a knob.  Include details regarding an IPSP and an EPSP example in the postsynaptic neuron. Also include clearing the synaptic cleft (gap). Use the back of this page...
Discuss and show one example of how the present value formula is a good method to...
Discuss and show one example of how the present value formula is a good method to determine how much is needed to save monthly, in order to have a specified sum of money at retirement age in 25 years at 8% interest. Discuss and show one example of how the future value formula is a good method to determine how much of a lump sum is needed today, to invest for 25 years to reach a specified retirement amount, with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT