collegegirl
asked on
Pascal (i think) passing by name, reference, value and value-result
Hi,
This may be kind of long but hopefully not too hard. This is the Pascal section so can you tell me if this program segment is written in Pascal or not (I don't know this language by the way...obviously).
program Main (...);
var Y: integer;
procedure P(X:integer);
begin X := X+1; write (X,Y) end;
begin
Y :=1; P(Y); write(Y)
end.
Is this in Pascal? This matters only so I ask this question in the right section. I don't know either way. What I do need to know is how to determine the following:
I know that passing by value means passing the parameter to the formal parameter; that passing by reference means that a pointer is used or has the potential to be used by a subprogram; passing by value-result is when the formal parameter is a local variable with the same data type as the parameter; and that passing by name is a subprogram call that substitutes for the body of subprogram.
I know in theory what all of these mean but because of my ignorance in Pascal, how do I follow the execution of these 4 types of parameters? I know that if you follow the execution of this segment (by transmitting Y to P) using each one of these methods (by reference, by name, etc.), the program will output several numbers.
Can you give me tips/resources/any help on how to determine what the output of this program segment would be using each of these four methods? If this segment is not in Pascal can you tell which language it is so I can ask this question in the right section?
I appreciate it so much. Thank you :)
Angela
This may be kind of long but hopefully not too hard. This is the Pascal section so can you tell me if this program segment is written in Pascal or not (I don't know this language by the way...obviously).
program Main (...);
var Y: integer;
procedure P(X:integer);
begin X := X+1; write (X,Y) end;
begin
Y :=1; P(Y); write(Y)
end.
Is this in Pascal? This matters only so I ask this question in the right section. I don't know either way. What I do need to know is how to determine the following:
I know that passing by value means passing the parameter to the formal parameter; that passing by reference means that a pointer is used or has the potential to be used by a subprogram; passing by value-result is when the formal parameter is a local variable with the same data type as the parameter; and that passing by name is a subprogram call that substitutes for the body of subprogram.
I know in theory what all of these mean but because of my ignorance in Pascal, how do I follow the execution of these 4 types of parameters? I know that if you follow the execution of this segment (by transmitting Y to P) using each one of these methods (by reference, by name, etc.), the program will output several numbers.
Can you give me tips/resources/any help on how to determine what the output of this program segment would be using each of these four methods? If this segment is not in Pascal can you tell which language it is so I can ask this question in the right section?
I appreciate it so much. Thank you :)
Angela
ASKER
Hi,
This is why I asked for tips/help/resources not for answers to the problem. I'm not asking you to do it for me but to help me understand how the execution works and how to determine the output using those methods because these concepts are hard for me to understand.
I don't really understand what: Procedure X(aValue:integer); and Procedure X(var aValue:integer); means? Can you give more explanation on this in layman's terms?
Thanks,
Angela
This is why I asked for tips/help/resources not for answers to the problem. I'm not asking you to do it for me but to help me understand how the execution works and how to determine the output using those methods because these concepts are hard for me to understand.
I don't really understand what: Procedure X(aValue:integer); and Procedure X(var aValue:integer); means? Can you give more explanation on this in layman's terms?
Thanks,
Angela
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
or see the accepted answer from:
https://www.experts-exchange.com/questions/21045612/newbie-object-pascal-question.html
https://www.experts-exchange.com/questions/21045612/newbie-object-pascal-question.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The 2 last of your parameter types are not pascal ('value result' and 'name'). They look like the earlier programming language Algol.
Pascal was very much derived from Algol, but Wirth chose to scrap those two as they answer only to a theoretical need. In practice only college professors could find a use for them. (it is worth noting that Java has now scrapped 'by reference' also. Simplicity is a valid goal in itself).
The code you show could be Pascal or Modula or some other language i dont know. Programmmmming languages borrow a lot from eachother.
regards JakobA
Pascal was very much derived from Algol, but Wirth chose to scrap those two as they answer only to a theoretical need. In practice only college professors could find a use for them. (it is worth noting that Java has now scrapped 'by reference' also. Simplicity is a valid goal in itself).
The code you show could be Pascal or Modula or some other language i dont know. Programmmmming languages borrow a lot from eachother.
regards JakobA
ASKER
Thank you so much for your help
Angela
Angela
The rest of it looks like homework though!
By value: Procedure X(aValue:integer);
By Reference: Procedure X(var aValue:integer);