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 1000Create a constant called
depositAmount with the value 250Write a function called
deposit that takes an inout parameter named accountBalance of type Int and a parameter amount of type IntInside the function
deposit, add amount to accountBalanceCall the function
deposit passing balance as an inout argument and depositAmount as the amountPrint 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