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

Posted on 2004-11-28
Last Modified: 2008-03-03
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 :)

Question by:collegegirl
    LVL 7

    Expert Comment

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

    Author Comment

    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?  


    LVL 8

    Assisted Solution

    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:

    LVL 8

    Expert Comment

    LVL 1

    Accepted Solution

    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
    LVL 10

    Assisted Solution

    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.
    LVL 15

    Expert Comment

    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

    Author Comment

    Thank you so much for your help


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Delphi Ascii code 2 934
    HTTP-POST Delphi 7 5 2,656
    Youtube stream inside Delphi 1 1,360
    Delphi 2007 printer setup problem 8 739
    This post first appeared at Oracleinaction  ( Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
    The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now