Functions challenge: parameters + return values
Learning intentions
In this challenge, you will combine everything from this level into one complete program.
- Use parameters to pass data into functions.
- Use return values to send results back.
- Choose clear function roles so logic is reusable.
You will also practise program structure.
- Build a menu loop with user choices.
- Keep input, calculation, and output in separate functions.
Challenge overview
You are building a small command-line app called Egg Shop.
- The app tracks egg stock and sales.
- The user can add eggs, sell eggs, and view stock summaries.
- All calculations must happen through functions with parameters and return values.
This is one longer task, not separate mini tasks.
- Plan your functions first.
- Then build the full program and test each menu option.
Program requirements
Your program must show this menu repeatedly until the user exits.
==== Egg Shop ====
1. Add eggs
2. Sell eggs
3. Show current stock
4. Show total eggs sold
5. Exit
Choose an option:
Use variables to track stock and sales, for example:
@main
struct SwiftPlayground {
static func main() {
var eggsInStock = 0
var eggsSold = 0
}
}
Required functions
Create and use functions like these (you may add more):
func menuChoice() -> Int
func addEggs(currentStock: Int, amount: Int) -> Int
func sellEggs(currentStock: Int, amount: Int) -> Int?
func updateSoldCount(currentSold: Int, amount: Int) -> Int
func stockMessage(stock: Int) -> String
Use parameters to pass in data.
- Example:
amountshould be passed in as a parameter, not hard-coded.
Use return values for calculated results.
addEggsreturns the new stock count.sellEggsreturns an optional because a sale may fail (can’t be completed).
Behaviour rules
Your app should handle normal and edge cases safely.
- If the user chooses an invalid menu option, show an error and continue.
- If the user enters a quantity less than
1, reject it. - If the user tries to sell more eggs than available stock, show an error and do not change values.
- Option 3 should print a clear stock message.
Suggested build order
- Write
menuChoice()and the mainwhileloop. - Implement
addEggs(currentStock:amount:). - Implement
sellEggs(currentStock:amount:). - Implement
updateSoldCount(currentSold:amount:). - Implement
stockMessage(stock:). - Connect each menu option and test all paths.
Test checklist
Run your app and verify each case.
- Add eggs at least twice.
- Try one invalid quantity (like
0or-3). - Sell eggs and confirm stock decreases.
- Try to sell more than stock and confirm it is blocked.
- Confirm total eggs sold updates correctly.
- Confirm the app exits cleanly when option
5is selected.
Extension for Super Players!
Improve your app with one extra feature.
- Add an option to reset the shop for a new day.
- Add an option to print a low-stock warning when stock is under a threshold.
- Add a function that returns a daily summary sentence.
Review
After completing the program, explain these in your own words.
- Why is
amountbetter as a parameter? - Why does
sellEggsreturn an optional? - Which parts of your program became easier to test because of return values?