Welcome back.
In this lecture I'm going to show you a couple of different styles of function declarations so you'll
understand how to declare your own functions in Go.
In the last lecture, you learned that using package level variables may create hard debug problems.
In this lecture, you're going to learn how to confine the variables within the scope of a function.
If the package variables are bad so what is the solution?
The solution is to localize and confine every variable as much as you can into the same function.
Let me show you a few examples in the coding editor.
First of all let me remove this code and let me also delete the bad.go file.
Now I'm going to put another window next to this one by clicking here.
Now both windows show the same main.go file. So you can better see what's going on here inside
the main function.
I'm going to declare a local variable by doing so.
I have confined the variable only to the main function no other function can access it but the main
function I'm going to declare another function inside I'm going to print the main function's local variable
as you can see it says the local variable is not declared.
This is good because I cannot access and change it without the consent of the main function.
Ok instead I'm going to declare an integer input parameter like so and I'm going to print the N variable
inside the main function.
I'm going to call it from here and I'm going to send the local variable to the show
function, good.
Now the show function can print the main function's local variable.
However what the show function prints here is not the local variable of the main function.
Let me show you what I mean here.
I'm going to declare a function named incrWrong.
Like so. It takes an integer parameter. Inside I'm going to increase it like so.
All right.
Let me call it from the main function.
Here I sent the local variable to the incrWrong function.
Let me also print it.
Why does it print 10 instead of 11?
It's because the n parameter here is a copy of the local (variable).
When you execute a function,
Go declares the input parameters as the local variables of that function. It declares a new variable named
n, and then it initializes it with the copy of the local variable.
That's why it cannot change the original variable.
This is what I mean by confining variables to a function.
So what should you do?
Well after incrementing the variable you need to return a new copy.
Like so.
However it says too many arguments to return because I try to return a value but the function doesn't
declare a result value so I cannot return from it.
Instead let's duplicate this function and I'm going to change its name to incr here.
I'm going to declare an integer result value like so now I can return from it because there is a result
value.
Now I need to call the incr function from the main function like so I'm going to print the local variable
using this show function it prints 10 again.
This happens because I didn't save the result of the function yet.
Let's do that.
It works now the local variable is 11.
Now let's say you want to increase the counter by a multiplication factor so let's declare another function
that takes two integers and returns a single integer like so.
This function returns a single integer so I am also going to return a single value.
Now I'm going to call it from the main function here.
I want to increase the counter with a factor of five.
And I copy the result back to the local variable.
Let me also print it.
Now the local variable is 55 so it prints 55.
Now let's say you want to increase the counter with a string value like so.
This function takes an integer and a string and it returns an integer and an error value.
It returns an error value because the factor variable may not be converted to an integer, as
you know.
First I'm going to call the Atoi function to check whether the factor can be converted to an integer
or not. Here,
I'm going to call the incrBy function because it already does the same job.
And lastly I'm going to return the result and the error value like so.
Since the function returns two result values, I need to use a comma to separate the result values.
OK it's time to use the function here inside the main(), I'm going to call the function like so. I've skipped
the first value because I passed an incorrect value.
So the function cannot convert it.
Let me check for the error, and print a message like so.
As you can see it prints an error. Let's duplicate it (the statement) and let's give it a convertible value like so
here I don't check the error value because I know for sure that it (incrByStr) can convert this value to an integer.
Let me also print the local variable. Now it works, and it prints 110. Here, it converts the string to
two.
Then it multiplies 55 by 2 and returns 110, hence the result.
Now I'm going to show you how to chain a couple of functions together. Here,
the incrBy function returns a single value.
Right.
And the show function takes an integer value.
So I'm going to directly give the result of the incrBy function to the show function like so. It
prints 220.
However it doesn't show you the current value of the local variable.
If I print the local variable here you can see that it still contains the same value 110.
This is because here the incrBy function returns a copy of it and the show function only prints the
copy.
By the way, can I do the same thing using the incrByStr function?
What do you think? Let me show you.
It says too many arguments because the show function expects a single value.
However, the incrByStr function returns multiple values so I cannot chain them.
Let me show you a more useful example.
Let's declare another function named sanitize.
It takes two values like so and it returns an int value.
As you can see the types of input values are the same as the incrByStr function.
This means that I can chain them.
I'll show you how to do that in a second.
First let's type a little bit of code in the sanitize function.
Let's say if there was an error you want to return zero like so.
Otherwise you return the given value.
First I'm going to call the sanitize function like so.
And I'm gonna give it the result of the incrByStr function. Let me print it. As you can see, it prints
zero.
It's because the incrByStr function returns an error.
So the sanitize function detects that and returns zero. In the end, I save zero to the local variable, so
it becomes zero instead.
Let me say 2 here.
Now, it prints 220. Here,
the incrBy function multiplies the copy of the local variable by 2.
Then it returns 220, and a nil error value to the sanitize function.
So the sanitize function sees that, so it just returns 220.
And lastly I save the result to the local variable.
So it becomes 220.
Let me show you another example.
Let's say you want to limit the result
if it is greater than a number.
So let's declare a function to do that.
It takes two integers and returns an integer.
And here is a named parameter. As you can see,
you can also name the result values. When you do so, they become the local variables inside the function, just
like the input parameters. Behind the scenes,
Go declares them like so.
So they get a zero value depending on their type.
So the n variable here is zero.
Okay first I'm going to change the m to the n.
I do so because if it (n) is not greater than the limit, I want to return the number (n) as it is.
So if the given number is greater than or equals to the limit, I'm going to limit it by setting it to
the limit value like so.
So the number cannot be greater than the limit.
In the end I'm going to return from the function like so. You might say: But you told me when a function
returns a result you need to give it to the return statement.
And I told you the truth. Here, behind the scenes, it returns the result like so. So, I don't have to.
I can do so because I use a named result value. Go allows you to do this.
All right let's switch over to the main function. First, I'm going to call the incrBy function with
a factor of 5. Let me also print it. As you can see, it prints 1100. Let's limit it to 500 by using
the limit function like so. OK, now it prints 500 because the limit function detects that it is greater
than 500 so it returns 500 instead.
As an example let me increase the limit to 2000. It prints like so because, 1100
is less than 2000.
All right.
That's all for now.
See you in the next lecture.