Phil H Posted May 3, 2014 Share Posted May 3, 2014 Hi, I am a 1st year computer science student doing my degree by distance education. I dont have anyone to bounce stuff off so any assistance would be really appreciated. I have been asked the following question............... The following is a program segment and the definition of a procedure named sub x <- 3; procedure sub (y) sub (x); y <- 5; print value of x; What value will be printed by the program segment if parameters are passed by value? Now I am taking it that the left is the calling program ( X ) and right is the procedure (Y ). So , to pass by value, program X sends its value of 3 over to procedure Y which leaves the value as 3. So the value 3 will be printed.................... And if it was passed by reference then X would pass 3 over to Y which would add the 3 to its value of 5.......... giving us a value of 8 passed back to the program on the left, which would then print a value X of 8............. Do I make sense? And am I on the right track. I do not want an answer. I just want to know if I understand, or not. Link to comment Share on other sites More sharing options...
Bignose Posted May 4, 2014 Share Posted May 4, 2014 I do not want an answer. I just want to know if I understand, or not. If I am reading everything correctly, I would say that you have not interpreted it correctly. Take more time to think about what the subroutine is inputting and outputting. Link to comment Share on other sites More sharing options...
Phil H Posted May 4, 2014 Author Share Posted May 4, 2014 So I am getting it the wrong way around? y sends 5 over and by value x prints out as 5. at the finish of this x is still 3 and y is still 5. If passed by reference then y passes 5 over and x prints as 8. at the finish of this, x is now 8 and y is 5. Link to comment Share on other sites More sharing options...
Bignose Posted May 4, 2014 Share Posted May 4, 2014 Again, I may be getting the notation used in the pseudocode wrong, but how I interpret the subroutine is that any object that is inputted is then set to the value of 5. So with that in hand, go through the steps of the main program. I don't think that there is anyway you should be getting 8, because there are no addition operators in this program as given. Link to comment Share on other sites More sharing options...
Phil H Posted May 4, 2014 Author Share Posted May 4, 2014 Bignose, I will reread the tests again. I see what you are saying. I was looking for the program to actually do something, but it appears to be much simpler than that. If passed by value then x would print as 3. If x was passed as reference then it would print as 5. Thank you. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now