Chapter 7 Quiz: Procedures and Functions

Test your understanding of procedures, functions, parameters, and procedural decomposition. Try to answer each question without looking back at the chapter, then check your answers at the bottom.


Question 1 (Multiple Choice)

What is the difference between a procedure and a function in Pascal?

A. A procedure can have parameters; a function cannot. B. A function returns a value; a procedure does not. C. A procedure can call other procedures; a function cannot. D. There is no difference — the terms are interchangeable.


Question 2 (True/False)

A procedure declaration must appear before the main begin...end block of the program.


Question 3 (Multiple Choice)

What keyword is used to declare a parameter that the procedure can modify, with the change visible to the caller?

A. const B. ref C. var D. out


Question 4 (Short Answer)

What punctuation mark ends a procedure's end keyword? What punctuation mark ends the program's final end keyword? Why is the distinction important?


Question 5 (Code Output)

What does the following program print?

program Quiz5;

procedure Mystery(X: Integer; var Y: Integer);
begin
  X := X + 1;
  Y := Y + 1;
end;

var
  A, B: Integer;
begin
  A := 10;
  B := 20;
  Mystery(A, B);
  WriteLn('A=', A, ' B=', B);
end.

Question 6 (Multiple Choice)

Which of the following is a valid reason to use a const parameter instead of a value parameter?

A. The procedure needs to modify the parameter. B. The parameter is a large string and you want to avoid copying it. C. The compiler requires const for all string parameters. D. const parameters can only be used with functions, not procedures.


Question 7 (True/False)

You can pass the literal value 42 as an argument to a var parameter.


Question 8 (Code Output)

What does the following program print?

program Quiz8;

function AddTen(X: Integer): Integer;
begin
  Result := X + 10;
end;

begin
  WriteLn(AddTen(5));
  WriteLn(AddTen(AddTen(3)));
end.

Question 9 (Short Answer)

Explain the difference between a parameter and an argument. Give an example of each.


Question 10 (Multiple Choice)

What is the purpose of a forward declaration?

A. To declare a variable that will be initialized later. B. To tell the compiler about a procedure's heading before its body is defined. C. To make a procedure visible outside the current source file. D. To optimize the procedure for faster execution.


Question 11 (Code Error)

The following code does not compile. Identify the error and explain how to fix it.

program Quiz11;

function Triple(const X: Integer): Integer;
begin
  X := X * 3;
  Result := X;
end;

begin
  WriteLn(Triple(7));
end.

Question 12 (True/False)

A nested procedure (declared inside another procedure) can access the local variables of its enclosing procedure.


Question 13 (Multiple Choice)

Which naming convention is recommended for procedures and functions?

A. Procedures should be named with nouns; functions with verbs. B. Procedures should be named with verbs; functions with nouns or adjectives. C. Both should use all-lowercase names with underscores. D. Both should be named with single letters for brevity.


Question 14 (Code Output)

What does the following program print?

program Quiz14;

procedure Modify(A: Integer; var B: Integer; const C: Integer);
begin
  A := 100;
  B := 200;
  { C := 300; — would not compile }
  WriteLn('Inside: A=', A, ' B=', B, ' C=', C);
end;

var
  X, Y, Z: Integer;
begin
  X := 1;
  Y := 2;
  Z := 3;
  Modify(X, Y, Z);
  WriteLn('Outside: X=', X, ' Y=', Y, ' Z=', Z);
end.

Question 15 (Short Answer)

What does the acronym DRY stand for, and how do procedures and functions help achieve it?


Question 16 (Multiple Choice)

Which parameter mode should you use for a string parameter that the procedure reads but does not modify?

A. Value parameter (no keyword) B. var parameter C. const parameter D. Any of the above will work equally well


Question 17 (Design)

You need to write a procedure that reads a real number from the user with validation (must be between 0.0 and 10000.0). Write only the procedure heading — choose appropriate parameter names and modes. Do not write the body.


Question 18 (True/False)

In Free Pascal, you can assign the return value of a function using either the function name or the Result variable.


Question 19 (Code Error)

The following program has a logic error (it compiles but does not work as intended). What is the problem?

program Quiz19;

procedure GetName(Name: string);
begin
  Write('Enter your name: ');
  ReadLn(Name);
end;

var
  UserName: string;
begin
  UserName := '';
  GetName(UserName);
  WriteLn('Hello, ', UserName, '!');
end.

Question 20 (Comprehensive)

List the three parameter-passing modes in Pascal. For each, state: (a) the keyword used, (b) whether the caller's variable can be modified, (c) one scenario where that mode is the best choice.


Answers

Answer 1: B. A function returns a value; a procedure does not. Both can have parameters and call other subprograms.

Answer 2: True. Pascal requires "declare before use" — procedures must be declared before the main program body and before any code that calls them (unless a forward declaration is used).

Answer 3: C. The var keyword declares a variable parameter that is passed by reference, allowing the procedure to modify the caller's variable. (ref is not a Pascal keyword; out exists in some dialects but was not covered in this chapter.)

Answer 4: A procedure's end is followed by a semicolon (;). The program's final end is followed by a period (.). This is important because using a period after a procedure's end tells the compiler the entire program has ended, causing everything after it to be ignored or to produce errors.

Answer 5:

A=10 B=21

X is a value parameter (copy), so the change to X does not affect A. Y is a var parameter (alias), so Y := Y + 1 changes B from 20 to 21.

Answer 6: B. A const parameter avoids copying large strings while also preventing accidental modification. The compiler may pass it by reference internally for efficiency. (A is wrong because const prevents modification; C is wrong because const is not required; D is wrong because const works with both procedures and functions.)

Answer 7: False. A var parameter requires a variable as its argument because the compiler needs a memory location to alias. Literals like 42 have no modifiable memory location.

Answer 8:

15
23

AddTen(5) returns 15. AddTen(3) returns 13, then AddTen(13) returns 23. Functions compose — the result of one call can be used as the argument to another.

Answer 9: A parameter is the variable declared in the procedure/function heading — it is part of the definition. An argument is the actual value or variable passed when the procedure/function is called. Example: in procedure Greet(Name: string), Name is the parameter. In the call Greet('Rosa'), 'Rosa' is the argument.

Answer 10: B. A forward declaration tells the compiler about a procedure's name, parameters, and return type before the full body is provided. This is needed when two procedures call each other (mutual recursion) or when you want to arrange code in a specific order.

Answer 11: The error is assigning to a const parameter: X := X * 3 is not allowed because X is declared as const. Fix: either remove const to make it a value parameter, or use a local variable: var Temp: Integer; ... Temp := X * 3; Result := Temp;.

Answer 12: True. Nested procedures can access the parameters and local variables of their enclosing procedure. This is called accessing the enclosing scope (or lexical scope).

Answer 13: B. Procedures should be named with verbs (they do things: PrintReport, ReadInput, SortArray). Functions should be named with nouns or adjectives (they produce values: Average, Maximum, IsValid).

Answer 14:

Inside: A=100 B=200 C=3
Outside: X=1 Y=200 Z=3

A is a value parameter — changing it to 100 does not affect X. B is a var parameter — changing it to 200 also changes Y. C is a const parameter — it cannot be changed (the commented-out line would not compile), and Z remains 3.

Answer 15: DRY stands for "Don't Repeat Yourself." Procedures and functions help achieve it by allowing you to write a piece of logic once and call it from multiple places. If you need to change the logic, you change it in one place, and all callers benefit. Without procedures, you would copy and paste code, creating multiple copies that must all be kept in sync — a common source of bugs.

Answer 16: C. const is the best choice for a string parameter that is read but not modified. It documents the read-only intent, prevents accidental modification, and avoids the overhead of copying the string (which a value parameter would do).

Answer 17:

procedure ReadValidAmount(const Prompt: string; var Amount: Real; Min, Max: Real);

Prompt is const because we only read it. Amount is var because the procedure must store the validated result in the caller's variable. Min and Max are value parameters — they are small Real values that we only read, so the copy cost is negligible.

Answer 18: True. In Free Pascal, both FunctionName := value and Result := value are valid ways to set the return value. Result is generally preferred in modern Pascal because it is clearer and avoids confusion with recursive calls.

Answer 19: The Name parameter is a value parameter (no var keyword). The procedure receives a copy of UserName, reads input into the copy, and when the procedure ends, the copy is discarded. The original UserName remains an empty string. Fix: Change the parameter to var Name: string so the ReadLn writes into the caller's variable.

Answer 20: | Mode | Keyword | Can Modify Caller's Variable? | Best Use Case | |------|---------|------------------------------|---------------| | Value | (none) | No (operates on a copy) | Small types (Integer, Real, Char, Boolean) that the procedure needs to read but not change externally | | Variable | var | Yes (alias to original) | When the procedure must return data through a parameter (e.g., reading validated input) | | Constant | const | No (compiler enforced) | Read-only access to large types (strings, arrays, records) where copying would be wasteful |