Practice Function (Programming) in CS Thinking
Use these practice problems to test your method after reviewing the concept explanation and worked examples.
Quick Recap
A named, reusable block of code that performs a specific task, taking input and optionally returning output.
A mini-program with a name. Call it by name whenever you need that task done.
Example 1
mediumDefine a function `double(n)` that returns n ร 2. Then trace `double(double(3))`.
Example 2
mediumWhy is it better to write a function `calculateTax(amount, rate)` than to repeat the tax calculation code everywhere?
Example 3
mediumWrite a function `isEven(n)` that returns TRUE if n is even, FALSE otherwise. Trace it for n = 7.
Example 4
mediumGiven `FUNCTION toSeconds(minutes): RETURN minutes * 60`, what does `toSeconds(3)` return, and why is using a function better than rewriting `minutes * 60` everywhere?