Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pascal

What are value parameters in Pascal?

Syed Muhammad Ali Mustafa

Parameters in Pascal are the variables passed to a function or procedure. We use parameters to pass data we want to use in a function or procedure. There are four types of parameters in Pascal:

Value parameters

Value parameters are the parameters that are passed by value. Passing parameters by value means that the function/procedure gets a copy of the parameters.

The changes made to the parameter passed by value are not reflected in the original variable when the function/procedure returns.

Syntax

Value parameters are passed to a function as follows:

function (_parameterName_: _parameterType_): _returnType_;
begin
   //function body
end

Similarly, value parameters are passed to a procedure as follows:

procedure (_parameterName_: _parameterType_);
begin
   //procedure body
end
  • _parameterName_: The name of the value parameter.
  • _parameterType_: The data type of the value parameter.
  • _returnType_: The data type of the return value of the function.

Note: The value parameters make heavy use of the stack as we make a copy of them. So, we must only use value parameters when required.

Code

Consider the code snippet below, which demonstrates the use of the value parameters.

program function1;
var
   a, b, calSum : integer;
   
function sum(a, b: integer): integer;
begin
   a := a + b;
   sum := a;//store return value in variable having same name as the name of function
end;

begin
   a := 5;
   b := 10;
   writeln('');
   writeln('a before calling function = ',a);
   calSum := sum(a, b);
   writeln(a, ' + ', b, ' = ', calSum);
   writeln('a after calling function = ',a);
end.

Explanation

A function sum() is declared in line 5 that calculates the sum of two numbers passed to it as parameters. The parameters passed to the sum() function are value parameters. This is why changing the value of the parameter a is not propagated back when the function returns.

RELATED TAGS

pascal

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring