Parameters CS Thinking Example 1
Follow the full solution, then compare it with the other examples linked below.
Example 1
easyWhat is the output of the following? FUNCTION greet(name): OUTPUT 'Hello, ' + name. greet('Alice'). greet('Bob').
Solution
- 1 Step 1: When greet('Alice') is called, the parameter name takes the value 'Alice'.
- 2 Step 2: The function outputs 'Hello, ' + 'Alice' = 'Hello, Alice'.
- 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.
About Parameters
Named values declared in a function definition that act as placeholders for the actual data (arguments) passed in when the function is called. Parameters allow the same function to operate on different data each time it is invoked.
Learn more about Parameters โMore Parameters Examples
Example 2 medium
FUNCTION calculateArea(length, width): RETURN length * width. What is calculateArea(5, 3)? What happ
Example 3 mediumWrite a function called power(base, exponent) that returns base raised to the exponent using a loop.
Example 4 hardExplain the difference between passing by value and passing by reference. If FUNCTION double(x): x =