Function (Programming) Examples: 47 Problems with Answers
Worked examples of Function (Programming) to try before you open each solution, then practice problems with answers.
Worked Examples
Example 1
mediumShow solution
Answer
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 each one, 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
challengeWhat 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.
Ask yourself: Is a named block written once and then run by calling it, maybe with inputs and a value sent back?
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 function is like a recipe card filed under a name. Writing the card cooks nothing; cooking happens only when someone pulls the card out and follows it (the call), using today's ingredients (the arguments), and hands over the finished dish (the return value). The same card works every day, with different ingredients each time.
Read the full lesson on Function (Programming)Related concepts
Background knowledge
These ideas may be useful before you work through the harder examples.