Welcome back!
In this part you're going to learn about functions and pointers.
All right let's start talking about the road map in this section.
You're going to learn about functions.
Functions are reusable code blocks that you can call any time you want.
You've been using them since the start of the course.
Now it's time to create your own functions in the next section you're going to learn about pointers
as well. A pointer store a memory address of a value such as a variable you can use pointers to manipulate
values indirectly through their memory addresses.
So at the end of this section you're going to rewrite the log parser project using functions.
You'll understand the basics of designing a program using functions.
You're also going to learn about the past by value semantics so you'll understand the implications of
passing struct, map and slice values to functions.
All right let's get started.
Now let's remember what a function looks like.
Every function starts with func keyword.
And it has a body that is wrapped between curly braces between these curly braces.
You write your code after the func keyword.
You need to give it a name.
There are some special functions called init and main if you don't want to hard to debug problems I
recommend you not to use their names in your custom functions by the way a function may declare zero or
more input parameters.
Right now this function doesn't have any input parameters.
So there are only parentheses for example.
This function doesn't declare any input parameters functions can also take multiple input parameters
like so each parameter should have a name and a type.
For example here is a function that adds two numbers together.
It has two input parameters and both are integers.
You can also type it like so.
So those two declarations are actually equal.
If you have noticed it works just like a parallel variable declaration if a function doesn't declare
any result values then you can call the return statement without any arguments like so by the way as
you know the return statement causes a function to stop and terminate immediately.
For example in this function when the return statement is executed the function won't execute the rest
of the code however most of the time a function may declare single or multiple result values as well.
You just need to declare a list of types like so separated by commas for example let's take a look at
the Atoi function, you know it very well. It takes a single input parameter and it returns two result
values an integer and an error as a convention you return an error value always declare it as the last
parameter when a function declares the result parameters it needs to return them as well.
To do that you need to use the return statement like so here this function returns multiple values.
So you need to separate them using a comma as you can see the number of values next to the return statement
matches the number of the result values when a function calls the Atoi function the returned values will be
copied to the caller function.
By the way if there is a single result value you don't need to wrap it inside parentheses for example
this function takes an int value and returns a single string value.
Notice that you don't need to use a comma when returning a single result value okay.
Now let's take a look at an example let's say you have two variables like so you want to multiply them
using a function like so as you can see unlike the main function you need to call your functions by
yourself or Go want execute them.
Let's take a look at what the multiply function looks like.
It accepts 2 input parameters and it returns a single result value let's execute the function the values
go into the function behind the scenes the function creates two new variables and assigns them the
input values.
So the variable a contains 5 and the variable b contains 2.
After that, the function multiplies the variable a with the variable b, and saves the result back into
the variable a.
Lastly it returns the copy of the variable a to the caller (function) like so so the variable rest at left becomes
10.
Notice that the variables n and m are the same.
It's because they don't belong to the multiply function.
So the function cannot change that the function can only change its own local copies here.
The variables a and b are local to the function.
So the function can only change the variables a and b.
For example here it changes the variable a.
After that it returns the variable a.
However it is also a copy.
So the return value is a clone of the local variable.
a. Alright.
Now let's take a look at an example in coding editor. As always,
we'll start with the most basics.
In the end you'll be able to write your own functions.
In this example you're going to declare very simple functions. At the start.
they won't take any inputs and they won't return anything.
The goal of this lecture is learning about the function scopes.
I'm going to show you why you should not use package level variables.
Okay let's say you want to create a program that keeps track of how many times a website gets visited.
To do that you create a package level variable like so as an example.
I'm going to create a couple of functions.
They will read and write
to this variable. Let's declare the first function. It doesn't take an input parameters and doesn't
return any value. It's gonna print the variable n. Let's also print the counter
let's say you want to print a message,
if the counter is zero like so. Okay let's see how it works.
As you can see it doesn't print anything because as I said only the main and init functions are called
automatically.
So you need to call the showN function yourself like so.
Hmm... I don't want to print the counter if it is zero right.
However here it gets printed.
Despite that so I'm going to go back to the showN function and here I'm going to return from it.
Remember this function doesn't return a result value so it cannot return a value the return statement
here only allows me to terminate the showN function.
So the function won't execute the rest of the statements because of the return statement it doesn't execute
the last statement.
Now I want to declare another function to increment the counter inside the function.
I'm going to increment it.
Now I'm going to call the function from the main function twice.
I'm also going to call the showN function here to print the counter.
OK good.
Now it says "n is two" because I called the incrN function twice by the way every declared function
occupies the same name space within the same package.
So you cannot redeclare a function with the same name of an existing function.
So in the same package each function should have a unique name.
As you can see now it says it was declared within the same scope before. By the scope,
it means the same package by the way if you are wondering about it.
Go doesn't support the function overloading anyway.
OK.
So far so good.
Now let's learn how to work with multiple files.
Let's create another file here.
I'm going to name it bad.go
and I'm going to move all the functions but the main function to the new file
Oops! It doesn't work.
The compiler cannot see the functions magically because here I tell it to run only the main.go
file.
Instead I'm going to say go run dot.
So it's gonna run every file in the current directory.
Now if it works, good. However declaring a package variable is a bad practice as you can see this file
can access the N variable because this file and the main.go are in the same package.
Imagine a package with hundreds of files.
In that case, any function or any other file can easily change the N variable.
It can go out of control quite easily.
It's because everyone can access and change them so you cannot easily know which one of the functions
read and change the package level variables.
Alright let's talk about the solution in the next lecture.
Let's take a quick break here for now.
See you there, bye.