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 (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.
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.
Sense of Study hint: When creating a function, first decide what single task it should perform and give it a descriptive name. Then identify what inputs (parameters) it needs and what output (return value) it should produce. Keep functions short and focused on one responsibility.
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
mediumExample 4
mediumExample 5
mediumExample 6
mediumExample 7
mediumExample 8
mediumExample 9
hardExample 10
hardExample 11
hardExample 12
challengePractice Problems
Try these problems on your own first, then open the solution to compare your method.
Example 1
mediumExample 2
mediumExample 3
easyExample 4
easyExample 5
easyExample 6
easyExample 7
easyExample 8
easyExample 9
easyExample 10
easyExample 11
mediumExample 12
mediumExample 13
mediumExample 14
mediumExample 15
mediumExample 16
mediumExample 17
mediumExample 18
mediumExample 19
challengeExample 20
challengeExample 21
challengeExample 22
mediumExample 23
easyExample 24
easyExample 25
easyExample 26
easyExample 27
easyExample 28
mediumExample 29
mediumExample 30
mediumExample 31
mediumExample 32
mediumExample 33
hardExample 34
hardExample 35
challengeRelated Concepts
Background Knowledge
These ideas may be useful before you work through the harder examples.