Jump to content

[PASCAL] Procedure and function output parameters


CouldnoT
 Share

Recommended Posts

Procedure and function output parameters

 

In the previous usage of functions, we found that we can return only one value, which is the result of the function, but how can we return more than one value in functions or procedures? Let us do this experiment:

 

procedure DoSomething(x: Integer);
begin
 x:= x * 2;
 Writeln('From inside procedure: x = ', x);
end;
// main
var
 MyNumber: Integer;
begin
 MyNumber:= 5;
 DoSomething(MyNumber);
 Writeln('From main program, MyNumber = ', MyNumber);
 Writeln('Press enter key to close');
 Readln;
end.

 

In this example, the doSomething procedure receives x as an Integer value, then it multiplies it by two, and then finally it displays it.

 

In the main part of the program, we declared the variable MyNumber as an Integer, put the number 5 in it, and then passed it as a parameter to the DoSomething procedure. In this case, the MyNumber value (5) will be copied into the x variable.

 

After calling the function, X 'svalue will be 10, but when we display MyNumber after procedure calling we will find that it still holds the value 5. That means MyNumber and X have two different locations in memory, which is normal.

 

This type of parameter passing is called calling by value, which does not affect the original parameter MyNumber. We also could use constants to pass such values, e.g:

 

DoSomething(5);

 

Link to comment
Share on other sites

  • CouldnoT changed the title to [PASCAL] Procedure and function output parameters
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.