Function Examples in CS Thinking
Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Function.
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 and can optionally accept inputs (parameters) and return a result. Functions allow you to organize code into logical units, write a solution once, and invoke it from anywhere in the program.
Like a recipe: you name it, write it once, and call it whenever you need it.
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: Functions break programs into manageable pieces, enable reuse, and reduce repetition.
Common stuck point: Defining a function doesn't run it β you must call it. Parameters (defined) differ from arguments (passed in).
Sense of Study hint: When creating a function, give it a descriptive name that tells what it does. Define parameters for any data it needs from the caller. Use return to send a result back. Keep each function focused on a single taskβif it does too many things, split it into smaller functions.
Common Mistakes to Watch For
Before you work through the examples, skim the mistake guide so you know which shortcuts and sign errors to avoid.
Worked Examples
Example 1
mediumAnswer
First step
See the full worked solution + why-it-works coaching
SetupKey insightWhy it worksCommon pitfallConnection
Example 2
mediumExample 3
hardExample 4
hardPractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
easyExample 2
easyExample 3
easyExample 4
easyExample 5
easyExample 6
easyExample 7
easyExample 8
easyExample 9
mediumExample 10
mediumExample 11
mediumExample 12
mediumExample 13
mediumExample 14
mediumExample 15
mediumExample 16
mediumExample 17
mediumExample 18
challengeExample 19
challengeExample 20
challengeExample 21
easyExample 22
easyExample 23
easyExample 24
easyExample 25
easyExample 26
easyExample 27
easyExample 28
mediumExample 29
mediumExample 30
mediumExample 31
mediumExample 32
mediumExample 33
mediumExample 34
mediumExample 35
mediumExample 36
mediumExample 37
mediumExample 38
mediumExample 39
hardExample 40
hardExample 41
hardExample 42
challengeExample 43
challengeBackground Knowledge
These ideas may be useful before you work through the harder examples.