Parameters Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Parameters.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Named values passed into a function when calling it, allowing the same function to operate on different data.

The blanks you fill in when using a function. Like a form with fields.

Read the full concept explanation β†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Parameters make functions flexibleβ€”same code, different data.

Common stuck point: Parameter (in definition) vs. argument (in call)β€”often used interchangeably.

Worked Examples

Example 1

easy
What is the output of the following? FUNCTION greet(name): OUTPUT 'Hello, ' + name. greet('Alice'). greet('Bob').

Solution

  1. 1
    Step 1: When greet('Alice') is called, the parameter name takes the value 'Alice'.
  2. 2
    Step 2: The function outputs 'Hello, ' + 'Alice' = 'Hello, Alice'.
  3. 3
    Step 3: When greet('Bob') is called, name = 'Bob', so it outputs 'Hello, Bob'. Parameters allow the same function to work with different inputs.

Answer

Hello, Alice then Hello, Bob.
Parameters are variables listed in a function definition that receive values (arguments) when the function is called. They make functions reusable with different data.

Example 2

medium
FUNCTION calculateArea(length, width): RETURN length * width. What is calculateArea(5, 3)? What happens if you call calculateArea(5) with only one argument?

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

medium
Write a function called power(base, exponent) that returns base raised to the exponent using a loop. Trace power(2, 4).

Example 2

hard
Explain the difference between passing by value and passing by reference. If FUNCTION double(x): x = x * 2 is called with SET a = 5. double(a), what is the value of a after the call in each case?

Related Concepts

Background Knowledge

These ideas may be useful before you work through the harder examples.

function