Global variable visible before definition in Swift

I have code that does not compile:

func find( n : String, m: Int)
{
    num = m
    name = n
}

var name : String = "A" 
var num : Int = 1

find( n: "1", m:  1)

print( name)

and he behaves as expected. But friends, please tell me why the following code snippet works. The variable is namealso defined after the function, but does it work this time?

func find( n : String, m: Int)
{
//   num = m
   name = n
}

var name : String = "A" 
// var num : Int = 1

find( n: "1", m:  1)

print( name)

What is wrong with global variable definitions?

+5
source share
1 answer

Global variables are defined before methods are called (even including viewDidLoad). Therefore, your program works as usual, because as long as the variable is defined outside the scope of the method, it will be called before any method.

0
source

Source: https://habr.com/ru/post/1650922/