In: Computer Science
Describe the difference between passing an argument by value and passing an argument by reference. As part of your answer, include the impact of changing the contents of the parameter variable in the calling part of the program for each argument type.
Using the properly aligned (use spaces instead of tabs) pseudocode format given in the textbook, design a module called GetNumber which uses a reference parameter variable to accept an Integer argument. The module should prompt the user to enter a number and then store the input in the reference parameter variable.
Using the properly aligned (use spaces instead of tabs) pseudocode format given in the textbook, design a nested decision structure to perform the following:
If amount_1 is greater than 100 and amount_2 is less than 500, display the greater of amount_1 and amount_2..
Using the properly aligned (use spaces instead of tabs) pseudocode format given in the textbook, design a For loop that displays the following set of numbers in the order given:
100, 95, 90, 85, . . . , 0
HI,
Q.Describe the difference between passing an argument by value and passing an argument by reference.
Answer: When a function is called, twe can pass the arguments in a function 1. passed by value or 2. passed by reference.
1.Pass by value means that a copy of the actual parameter’s value is made in memory.
Note: Changes made to the passed variable do not affect the actual value in pass by value.
2. Pass by reference (also called pass by address) means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function so that a copy of the address of the actual parameter is made in memory. refeence means address of the value stored in perticular variable.
Note: Changes to the value have an effect on the original data.if we will fetch the variaable vy refrence then original copy of data will be effected
Program1: Accepting integer by user input and storing integer in reference variable:
#include<iostream>
using namespace std;
void getNumber (int& first, int x)
{
cout << "x = " << first << endl ;
cout << "refernce = " << x << endl ;
}
int main()
{
int x ;
cout << "Type a integer number: ";
cin >> x;
int& reference = x;
getNumber(x, reference );
return 0;
}
Program 2: Greated number b/w amount_1 and amount_2:
#include<iostream>
using namespace std;
int main()
{
int amount_1, amount_2;
cout << "Please enter amount 1" << endl ;
cin>> amount_1;
cout << "Please enter amount 2" << endl ;
cin>> amount_2;
if(amount_1>100 && amount_2<500 )
{
if(amount_1>amount_2)
{
cout<<"Amount 1: "<<amount_1<<" is the largest";
}
else
{
cout<<"Amount 2: "<<amount_2<<" is the largest";
}
}
else
{
cout<<" Incorrect input.Enter the correct numbers";
}
return 0;
}
Program 3: For loop to displat series 100, 95, 90, 85, 80, 75,........................, 15, 10, 5, 0,
#include <iostream>
using namespace std;
int main() {
for (int x=100; x>=0; x=x-5) {
cout << x << ", ";
}
return 0;
}
Thanks