Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


What are constant parameters in Pascal functions?

Amaaz Ahmad

A constant parameter, declared by the keyword const, is a read-only parameter. This means that we can not modify the value of the constant parameter in the function body.

Using the const keyword tells the compiler that the value of that parameter will not be changed inside the function. Hence, the compiler can make optimizations that would not have been possible otherwise.

A constant parameter cannot be passed as a variable argument to another subroutine.

Moreover, a constant parameter can be passed by value or by reference, depending on the compiler. We can use the [Ref] keyword with const if we want to ensure that a constant parameter is passed by reference. This can decrease the cost of generating a copy while the function is being executed.


For a function:

function functionName (const parameterNames: dataType...) : returnType;

For a procedure:

procedure procedureName (const parameterNames: dataType...);

In Pascal, a function is a subroutine that returns a value, whereas a procedure does not.

In the above snippets of code, functionName or procedureName are replaced by the function or procedure names, respectively. The names of the constant parameters are written in place of parameterNames, and their data type is written in place of dataType.

If we want to pass multiple constant parameters of the same type, using the const keyword once and then writing the names of all variables is enough. This is also shown in the example below.


Program ChangeANum(output);

  num1 : integer = 5;
  num2 : integer = 10;
  num3 : integer = 15;
Procedure ChangeNum(Const n, m : integer; x : integer);  
//    n := 6;
//    m := 9;
    x := 50;

  writeln('Values inside the function : ');
  writeln('NUM-1: ', n, '    NUM-2: ', m, '    NUM-3: ', x);
  writeln('Values before the function call: ');
  writeln('NUM-1: ', num1, '    NUM-2: ', num2, '    NUM-3: ', num3);
  changeNum(num1, num2, num3);

In the above snippet of code, we’re passing two constant parameters and one value parameter into the changeNum function.

A value parameter is a parameter that can be changed and is passed by value. Any changes made in its value inside the function are not reflected outside the function. More about value parameters can be found here.

We used the const keyword once to make m and n both constant parameters. In this case, x is not a constant parameter.

Inside the function body, as m and n are constant parameters, assigning any value to them will result in a compilation error. Line 11 and 12 are commented to avoid this compilation error. In the case of the parameter named x, we can assign a value to it as it is not a constant parameter, as shown in line 13.

In the main block of code, the changeNum function is called by passing num1, num2, and num3 as arguments.




Amaaz Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring