In How to Make an App in 8 days, Lesson 7, Chris puts the function after the body code, but calls the function inside the body. I thought he said in a previous lesson that the code gets executed in order from top to bottom, so how come the function works when it gets called before the function is created?
You’re partially got it! Code does execute top to bottom.
But looking at a
View this is a structure. A
body is a property of the view, and what’s displayed on the screen.
Functions can be inside a structure. Like what you’re seeing, but the function isn’t executed until it’s called from the body.
The order of the variables and functions inside of a structure don’t matter!! Code in a function is what matters, top to bottom.
Thank you! That’s helpful to know. I was just confused about how a function can be called before it is created. When Chris was talking about code being executed from top to bottom, was he only referring to functions, then?
Yes! Functions and a Swift playground
A class or structure, they are objects and actually all loaded at once (so variables and functions are loaded at a single time), but for functions to “work” they need to be “called” from somewhere
Think of it like reading a book, to read the book, you have to open it. The pages are all there with words, but you can’t read the book unless you open it (call the function)