Parameter passing

I am doing a college project and as part of it I have to use parameter passing between procedures and functions, this is supposed to keep global variables to a minimum. What is parameter passing?

Comments

  • you can write procedures/functions that need parameters

    eg:
    Maths :
    function Sin : Real -> Real

    Pascal :

    FUNCTION Sin(Argument : REAL) : REAL;

    Eg:

    Type TPoint = RECORD X,Y : REAL; END;

    function GetFirstCoord(Point : TPoint) : REAL;
    BEGIN
    GetFirstCoord:= Point.X;
    END;

    of course, procedures/functions can take more than one parameter.

    I think you should see the help by pressing CTRL+F1
    DexterMagnigfic at yahoo.fr

  • : I am doing a college project and as part of it I have to use parameter passing between procedures and functions, this is supposed to keep global variables to a minimum. What is parameter passing?
    :

    Ahh I love how teachers like to group global variables with GOTO and say they should never be used. In my opinion they are no different than anything else...when used properly they can be very useful, its only when used improperly that they cause problems.

    But anyway you didnt want a rant so I'll stop. It's probably much easier to show you than to explain what the difference between using global variables and using parameter passing is. So here is a "good" and "bad" example.

    The "bad" global variable method:

    [code]
    program Test;

    var
    UserName: String;

    procedure GetName;
    begin
    ReadLn(UserName);
    end;

    procedure WriteName;
    begin
    WriteLn(UserName);
    end;

    begin
    GetName;
    WriteName;
    end.
    [/code]

    Now the "good" parameter passing version would be:

    [code]
    program Test;

    { We turn this into a function }
    function GetName: String;
    var
    S: String;
    begin
    ReadLn(S);
    GetName := S;
    end;

    { We add a parameter to this procedure }
    procedure WriteName(AName: String);
    begin
    WriteLn(AName);
    end;

    { We move the UserName variable down to
    right above the main begin..end. block
    so it is no longer global }
    var
    UserName: String;
    begin
    { And we modify the calls here to
    correspond with the above changes }
    UserName := GetName;
    WriteName(UserName);
    end.
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories