Passing Data Into Subprograms
Pascal parameters come in two flavors that determine how data flows between a caller and a subprogram: value parameters, which receive a copy of the argument, and variable (var) parameters, which receive a direct reference to the caller's actual variable. Choosing between them is a deliberate design decision — value parameters protect the caller's data from modification, while var parameters are the mechanism Pascal uses to let a subprogram hand back more than one result.
Cricket analogy: Handing a substitute fielder a photocopy of the team's fielding chart lets them mark it up freely without affecting the original, like a value parameter, while handing them the actual chart on the captain's clipboard means their edits are the real plan, like a var parameter.
Value Parameters
Declared without the var keyword — procedure Adjust(x: Integer); — a value parameter copies the argument's value into a fresh local variable when the subprogram is called. Any assignment to that parameter inside the subprogram body affects only the local copy; the caller's original variable is completely unaffected, which makes value parameters the safe default for data a subprogram only needs to read or work with temporarily.
Cricket analogy: A batting coach adjusting a player's stance on a video-analysis freeze-frame doesn't change how the player actually stood during the match, just as modifying a value parameter inside a procedure never changes the caller's original variable.
procedure DoubleValue(x: Integer);
begin
x := x * 2;
WriteLn('Inside DoubleValue, x = ', x);
end;
procedure DoubleVar(var x: Integer);
begin
x := x * 2;
end;
var
a, b: Integer;
begin
a := 5;
b := 5;
DoubleValue(a);
DoubleVar(b);
WriteLn('a = ', a, ', b = ', b); { a = 5, b = 10 }
end.Variable (var) Parameters
Declared with the var keyword — procedure Adjust(var x: Integer); — a var parameter binds directly to the caller's actual variable rather than a copy. Any assignment inside the subprogram is immediately visible to the caller after the call returns, which is exactly how Pascal implements 'out' and 'in-out' parameters, since the language has no separate return-by-reference syntax for functions.
Cricket analogy: A physio directly re-taping an injured player's actual ankle mid-match changes the real state the player takes back onto the field, just as a var parameter's modification is visible to the caller immediately after the call.
Because var parameters expose the caller's real variable, an unintended assignment inside the subprogram silently corrupts the caller's data with no warning — always double-check whether a parameter truly needs var, since it removes the safety that value parameters provide by default.
Scope and Nested Blocks
Pascal is block-structured: variables declared in a program's outer var section are global and visible everywhere, while variables declared inside a procedure or function's own var section are local, existing only for the duration of that call and invisible outside it. Procedures can be nested inside other procedures, and an inner block can see (and shadow) identifiers from any enclosing block — if a local variable shares a name with a global one, the local declaration takes precedence within its own scope, silently hiding the outer identifier for the rest of that block.
Cricket analogy: A team's overall strategy board is visible to every player, global scope, but a specific fielding huddle's hand signals are only understood by the players in that huddle, local scope, and if a substitute uses the same signal name for something different, the huddle's meaning takes precedence during that conversation, just like variable shadowing.
var
counter: Integer;
procedure Increment;
var
counter: Integer; { shadows the global counter }
begin
counter := 100;
WriteLn('Local counter = ', counter);
end;
begin
counter := 1;
Increment;
WriteLn('Global counter = ', counter); { still 1 }
end.Shadowing is not an error in Pascal — it's legal and sometimes intentional — but it's a common source of bugs when a programmer forgets a local variable has the same name as an outer one and is surprised the outer variable never changes.
- Value parameters (declared without var) receive a copy; changes stay local to the subprogram
- var parameters bind to the caller's actual variable; changes are visible after the call returns
- var parameters are Pascal's mechanism for returning more than one value from a subprogram
- Global variables declared in the outer var section are visible throughout the program
- Local variables declared inside a procedure exist only for that call's duration
- A local variable with the same name as a global one shadows the global within its scope
- Shadowing is legal in Pascal but is a common, hard-to-spot source of bugs
Practice what you learned
1. What is copied when an argument is passed to a value parameter?
2. How is a var parameter declared in a Pascal procedure header?
3. Why does Pascal use var parameters instead of a return-by-reference syntax for functions?
4. What does variable shadowing mean in Pascal's scoping rules?
5. After calling a procedure with a value parameter that reassigns the parameter, what happens to the caller's original argument?
Was this page helpful?
You May Also Like
Procedures and Functions
Learn how Pascal separates reusable code into procedures (no return value) and functions (return a value), and how to declare and call each.
Recursion in Pascal
Learn how Pascal functions and procedures can call themselves, and how to design correct base cases and recursive cases.
Loops in Pascal
Master Pascal's three loop constructs — for, while, and repeat-until — and know when to use each.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics