Function (Programming) CS Thinking Example 3

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

Example 3

medium
Write a function `isEven(n)` that returns TRUE if n is even, FALSE otherwise. Trace it for n = 7.

Solution

  1. 1
    Step 1: FUNCTION isEven(n): RETURN n MOD 2 == 0.
  2. 2
    Step 2: isEven(7): 7 MOD 2 = 1, 1 == 0 is FALSE. Return FALSE.

Answer

FALSE. 7 is not even.
Functions that return boolean values are called predicates. They are commonly used in conditions and filters.

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