Question

Solve this in c++ must read question carefully take one value only i think so,,,,,, otherwise...

Solve this in c++ must read question carefully take one value only i think so,,,,,, otherwise you know better need solution urgently............

Write a program that pass two parameters to a function one by value and one by reference. Then check the original values whether their values have been changed or not.

Homework Answers

Answer #1
  • Theory of pass-by-value and pass-by-reference:
    • In pass-by-value method, when the function takes the argument,, it maked a copy of the argument and does operations on this copy. Any changes made to the copy will not be reflected on the original variable
    • In pass-by-reference method, when the funciton takes the argument, it takes a reference to this variable. Any changes made to the variable in the function will be reflected in the same variable in the calling function. pass-by-reference parameters are explicitly mentioned in the parameter list using & symbol after the datatype of the variable.
  • In this program, we have demonstrate these two methods using a single fucntion call. This function will be taking two varaibles, one passed by value and another passed by reference.

program:

#include <iostream>

using namespace std;

void changeValues(int x, int& y){
   x += 5;
   y += 5;
}

int main(){
   int x = 5, y = 5;
   cout<<"Original Values:\n";
   cout<<"x = "<<x<<endl;
   cout<<"y = "<<y<<endl;
   changeValues(x,y);
   cout<<"Values after modification:\n";
   cout<<"x(passed by value) = "<<x<<endl;
   cout<<"y(passed by reference) = "<<y<<endl;
   return 0;
}

  • Note that x is being passed by value and y is being passed by reference. Any changes made to y in changeValues() will be reflected for y in main(). But, this wont happen for x.

output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n):...
Implement the following functions in the given code: def random_suit_number(): def get_card_suit_string_from_number(n): def random_value_number(): def get_card_value_string_from_number(n): def is_leap_year(year): def get_letter_grade_version_1(x): def get_letter_grade_version_2(x): def get_letter_grade_version_3(x): Pay careful attention to the three different versions of the number-grade-to-letter-grade functions. Their behaviors are subtly different. Use the function descriptions provided in the code to replace the pass keyword with the necessary code. Remember: Parameters contain values that are passed in by the caller. You will need to make use of the parameters that each...
---------------- Overview: ---------------- This lab will consist of several independent exercises that must all use recursion...
---------------- Overview: ---------------- This lab will consist of several independent exercises that must all use recursion to solve various problems. Some problems will have fairly short solutions, but declaring and using classes/objects is still required. Some could benefit from having some kind of data structure being passed between calls. Feel free to use your LinkedList if you think it could help. Some solutions will be a mix of iteration and recursion, but all solution need to be recursive in some...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
Need in C language also need full documentation/explanation of each line A student has established the...
Need in C language also need full documentation/explanation of each line A student has established the following monthly budget: Budget Categories Budgeted amount ----------------------------------------------------- Housing $ 580.00 Utilities $ 150.00 Household Expenses     $ 65.00 Transportation $ 50.00 Food $ 250.00 Medical $ 30.00 Insurance $ 100.00 Entertainment $ 150.00 Clothing $ 75.00 Miscellaneous $ 50.00 ---------------------------------------------------- Write a program that declares a MonthlyBudget structure designed with member variables to hold each of these expense categories. The program should define...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour notation. For example, it should convert 14:25 to 2:25 PM. (A) The user provides input as two integers separated by ‘:’. The following function prototype should capture the user inputs as described below: void input(int& hours24, int& minutes); //Precondition: input(hours, minutes) is called with //arguments capable of being assigned values. //Postcondition: // user is prompted for time in 24 hour format: // HH:MM, where...
My Homework: look at any one topic covered in the discussion boards making sure the topic...
My Homework: look at any one topic covered in the discussion boards making sure the topic generated a lot of interesting class participation. Re-read the comments of your peers and your original post: what do you think about the issue now? Has the conversation changed your view point or only strengthened it? Why? Finally, what are the things you can take away from the course overall? Are there specific concepts and ideas that were eye-opening to you personally? Do you...
Read the Following Case Study and Answer the Question Below Mandy L. Georgia I live near...
Read the Following Case Study and Answer the Question Below Mandy L. Georgia I live near a major highway on a mini-farm with chickens, ducks and goats. I have a domestic partner and two wonderful sons. I now love to sit on my porch, watching the chickens peck the ground and enjoying the sunshine. But, not too long ago, I used to sit on this same porch and think how easy it would be to walk into traffic. I suffer...
I need the actual code for this... I found a similar response but it was not...
I need the actual code for this... I found a similar response but it was not thorough enough. Problem Prerequisites: None Suppose that a scientist is doing some important research work that requires her to use rabbits in her experiments. She starts out with one adult male rabbit and one adult female rabbit. At the end of each month, a pair of adult rabbits produces one pair of offspring, a male and a female. These new offspring will take one...