0
0
Swiftprogramming~30 mins

In-out parameters for mutation in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
In-out Parameters for Mutation in Swift
📖 Scenario: You are working on a simple app that manages a user's bank account balance. Sometimes, you need to update the balance by adding or subtracting money directly.
🎯 Goal: Build a Swift program that uses an inout parameter to change the bank account balance inside a function.
📋 What You'll Learn
Create a variable called balance with the value 1000
Create a constant called depositAmount with the value 250
Write a function called deposit that takes an inout parameter named accountBalance of type Int and a parameter amount of type Int
Inside the function deposit, add amount to accountBalance
Call the function deposit passing balance as an inout argument and depositAmount as the amount
Print the updated balance
💡 Why This Matters
🌍 Real World
Banking apps and financial software often need to update account balances safely and clearly. Using <code>inout</code> parameters helps functions change values directly.
💼 Career
Understanding how to use <code>inout</code> parameters is important for Swift developers working on apps that manage data that changes, such as user profiles, game scores, or financial records.
Progress0 / 4 steps
1
Create the initial balance and deposit amount
Create a variable called balance and set it to 1000. Then create a constant called depositAmount and set it to 250.
Swift
Need a hint?

Use var for balance because it will change. Use let for depositAmount because it stays the same.

2
Write the deposit function with an inout parameter
Write a function called deposit that takes an inout parameter named accountBalance of type Int and a parameter amount of type Int. Inside the function, add amount to accountBalance.
Swift
Need a hint?

Remember to use inout before the parameter type to allow mutation inside the function.

3
Call the deposit function to update balance
Call the function deposit passing balance as an inout argument and depositAmount as the amount.
Swift
Need a hint?

Use & before balance when passing it to the inout parameter.

4
Print the updated balance
Write a print statement to display the updated value of balance.
Swift
Need a hint?

Use print(balance) to show the new balance.