• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 751
  • Last Modified:

Pascal (i think) passing by name, reference, value and value-result

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;
     Y :=1; P(Y); write(Y)

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 :)

3 Solutions
It certainly looks like pascal!

The rest of it looks like homework though!

By value:    Procedure X(aValue:integer);
By Reference:  Procedure X(var aValue:integer);
collegegirlAuthor Commented:
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?  


just try google with the keywords  "by reference pascal function procedure" to get
many info from the net.
eg. read this short link , It will answer all of your questions:
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

TO start with, yes thats pascal!

A procedure is a small chunk of code set to perform a certain task such as input a name etc. that you can call to save time when your coding. e.g. a section called read_name then call it with read_names; is much faster than having a plethora of readln(); all through the code.

where you have Y : Integer its just making Y a value which can only hold whole numbers (integers), the same with X : Integer inside the procedure.

procedure P(X:integer);
Y :=1; P(Y); write(Y);

I think those two lines are confusing you the most.

To step it through, i will comment the code for you (with {})

program Main (...);                     {this just names the program name}
     var Y: integer;                       {declaring the value Y, as an variable integer value, it can have any value set to it, that is a whole number}
     procedure P(X:integer);         {opens the procedre, called P, with the parameter X set as an integer for use only inside that procedure}
          begin X := X+1; write (X,Y) end; { simple mathematical operations}
     begin                                    {this is where the actual program starts, the procedure isnt processed until it is called}
     Y :=1; P(Y); write(Y)             {it declares the variable integer Y, to a value on 1, then it calls the procedure P and passes the value Y to the procedure. the procedure received the value Y, and it can process it how it wishes, then the value of Y is passed back to the main program, where it is written to the screen}

Some basic pascal syntax:

var --- declare a letter/name to be allowed to have values assinged to it, can be integer, real, string, and others
begin...end --- all programs, and procedures must be wrapped with begin and end to show where they are to be executed
write ----output data to teh screen

Hope that explains
Pascal keeps values of the variables on program stack.

Declaration of the variable means that compiler entering the block of the code (where variable is declared) creates the variable by allocation of proper amount of memory on the program stack. Then the variable can be accessed by it's name.

Declaring by the value:
Procedure X(aValue:integer);

When entering the procedure, pascal allocates memory for the new variable aValue. When procedure ends aValue is destroyed.
After new variable is created it is filled with the value passed in procedure call.
X (1234);
Means aValue has an initial value of 1234.

Declaring by reference:
Procedure X(var aValue:integer);

When entering the procedure no memory allocation is performed. All operations with aValue are performed on the variable passed wit procedure call:
X (PassedVariable);
Every operation on aValue is in the fact operation with PassedVariable.
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
collegegirlAuthor Commented:
Thank you so much for your help


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now