Function (Programming) Examples in CS Thinking
Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Function (Programming).
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
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.
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: Write once, use many times. Functions take input (parameters) and give output (return value).
Common stuck point: Defining a function doesn't run itβyou must call the function.
Worked Examples
Example 1
mediumSolution
- 1 Step 1: FUNCTION double(n): RETURN n * 2.
- 2 Step 2: Inner call: double(3) = 3 * 2 = 6.
- 3 Step 3: Outer call: double(6) = 6 * 2 = 12.
Answer
Example 2
mediumPractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
mediumExample 2
mediumRelated Concepts
Background Knowledge
These ideas may be useful before you work through the harder examples.