Swift 4 - How to have a function set a variable outside that function

Swift 4 - How to have a function set a variable outside that function

So I just started with Swift the other day. In messing around with some code, I tried to write a function that would have a target player attack and enemy and have that enemies HP reduced by the amount of attack power of the player. Everything works inside the function fine, but I can't seem to find a way to get it to effect the selected enemy outside the function. Maybe its something that will come in a later lesson, but in my head it should be something simple that I can't seem to figure out.

So I just started with Swift the other day. In messing around with some code, I tried to write a function that would have a target player attack and enemy and have that enemies HP reduced by the amount of attack power of the player. Everything works inside the function fine, but I can't seem to find a way to get it to effect the selected enemy outside the function. Maybe its something that will come in a later lesson, but in my head it should be something simple that I can't seem to figure out.

Here is the code that I am using.

struct Player {
    var maxHP: Int
    var currentHP: Int
    var maxMP: Int
    var currentMP: Int
    var currentLVL: Int
    var damageATT: Int
}

struct Enemy { var maxHP: Int var currentHP: Int var damageAtt: Int }

var alexRider = Player(maxHP: 100, currentHP: 100, maxMP: 100, currentMP: 100, currentLVL: 1, damageATT: 5) var enemyOne = Enemy(maxHP: 20, currentHP: 20, damageAtt: 2) var enemyTwo = Enemy(maxHP: 40, currentHP: 40, damageAtt: 4)

func attackTarget(attacker player: Player, enemy target: Enemy) { var attackTAR = target attackTAR.currentHP -= player.damageATT print("Dealt (player.damageATT) damage!") print("Enemy now has (attackTAR.currentHP) health remaining") }

attackTarget(attacker: alexRider, enemy: enemyTwo) print(enemyTwo.currentHP)

Obviously, when I print the currentHP at the end of the code, its the same as it started since the scope of the variables inside the function do not pass outside. The print commands inside the function work fine to show the damage amount and remaining HP of the instance inside.

Is there something simple I am missing here? Again I know this should be something simple, but just getting started with this I was hoping someone could point me in the right direction.

Thanks.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Access variable from function inside of nested function

In PHP, you have to use the global keyword in order to access a variable from the global scope when inside of a function. However, how can you access a variable within the scope of a parent function?

Swift 5 Tutorial 2020 - Learn Swift 5 for Beginners

Swift 5 is the programming language used to make iPhone, iPad and MacOS apps. In this Swift 5 Tutorial 2020 - Learn Swift 5 for Beginners, I'll cover everything you'd learn about Swift 5 from a 500 page book in one video