We need to talk about nested functions, let's say you have a function like this called Two Steps Forward,
one back, and we pass in a distance of steps, which then returns an integer.
And what this function is going to do is take literally two steps forwards and then one back multiplied
by the distance that you pass in.
Okay.
And if you're not from the English speaking world, perhaps you don't recognize this, but it's a common
I don't know, it's in common parlance, in common English.
Sometimes things are so hard in life.
You take two steps forwards, but you always slide one back.
And I put coding in that category.
When you're just learning, it's pretty difficult to learn it.
Right.
How would we do this?
Well, most people would type a line of code, a line of code, a line of code, etc..
OK, but what we can do is we can nest functions so we can declare a function inside of a function.
And I've got a couple here.
This is the forward one, so we have a function forward which goes a distance and passes and passes
out an integer.
Now this is all mashed together in one line.
We can actually do this.
So for Swift, it doesn't actually matter if you're all in one line or not, because it'll evaluate
the same way, other languages don't necessarily do that.
So be careful in that respect.
It's what this function forward does is return the distance times to because two steps forwards, then
we have a function for backwards.
Can you guys hear that plane outside?
Because I sure can it's really loud to me, but you probably can't hear it.
OK, then we have a function for backward which returns the distance times minus one, because two forwards,
one backwards.
Backwards is negative.
Now, I cannot call those functions outside of the original function, so I can't say forward.
It just doesn't exist.
Same for backward, nothing that.
But I can actually call them inside here.
And I can pass in the distance, I can also call backward.
Pass in the distance.
So we want to have forward and backward and what have I got here?
Yes, we're going to add them.
So put this onto the next line at that, and then we're going to return it.
So if we run that function.
With two, we should get an answer of two, because it'll go four steps forward and two back against
to.
And there's the answer we have to now, obviously, the way the maths of this works out is we just simply
get back, I think the number that we originally put in.
So it's for.
That's just the way the math works out, but my point is a nested function sits inside a function.
Now, you're going to come across this concept quite often, but it won't necessarily have the function
keyword attached to it.
What you have to recognize is when you have these curly braces and that tells you that what you're looking
at is a function, whether it's named or it's anonymous doesn't matter.
You're looking at a nested function.
I tend not to use nested functions, to be honest, I don't think they're very useful in terms of structuring
your code and they might confuse things.
But as always, there are use cases for everything.
Right.
Time for you to do the same thing.
Here's your homework and make a function to add two numbers.
Use a nested function to check.
The answer is less than 10 and return greater than if it is.
That should be less than it.
And return less than if it is less than 10 or greater than 10, if it's not less than 10, right.
So a lot of confusing words there, I think even now that I've done it.
But I'm saying it I feel pretty confused reading it and see what you can make of that and let me see
what you come up with.