Function (Programming) CS Thinking Example 4

Follow the full solution, then compare it with the other examples linked below.

Example 4

medium
Given `FUNCTION toSeconds(minutes): RETURN minutes * 60`, what does `toSeconds(3)` return, and why is using a function better than rewriting `minutes * 60` everywhere?

Solution

  1. 1
    Step 1: Substitute the input value: toSeconds(3) = 3 * 60 = 180.
  2. 2
    Step 2: Using a function is better because the logic is written once and can be reused, updated, and tested in one place.

Answer

180. A function improves reuse, readability, and maintainability because the conversion rule lives in one named block.
Functions package a task behind a clear name. That reduces repetition and makes programs easier to understand, change, and debug.

About Function (Programming)

A named, reusable block of code that performs a specific task, taking input (parameters) and optionally returning output (a return value). Functions allow you to write a piece of logic once and use it many times throughout a program.

Learn more about Function (Programming) โ†’

More Function (Programming) Examples