In this venue we will learn about one of the most fundamental concept in any programming language functions
functions are named section of a program that performs a specific task they can receive zero or more
arguments in return zero or more values.
Let's see some examples of defining functions.
So the simpler one will receive no argument and return no value.
We define a function by starting using the func keyword then the name of their function.
This case let's see.
Hello between parentheses are going to be the arguments in this case were not returned receiving any
arguments and we're not returning any value in which in brackets we will define the behavior of this
function.
The behavior of this function is just to print Hello.
So let's type print a land which is a building function.
Go provide this function and let's type the argument which is gonna be hello now to call the function
would just type the name hello and we open and close parentheses.
Let's save this and executed it Hello.
If we want to repeat the operation we just copy the line run the program again and we have Hello twice.
Now let's say we want to define a function that receive arguments let's build some function which is
gonna add two different numbers to func to say that we're defining a function the name of the function
in this case some and let's receive two arguments two numbers x which is an integer and Y which is an
integer.
Now this function should return the operation of adding these two numbers so it will return an integer
as well after the parentheses you can define the return value now just a tip here.
Ask X and Y are from the same type.
You can all meet the first type.
And basically these red X and Y are from into type.
Now let's write the behavior.
So this function returns the value to return values.
We need to write return.
And now what do we want to do to add X and Y.
Right now let's call this function.
Some and now we can pass two different arguments.
Let's say 10 and 20.
And print output to print the sum of 10 and 20.
We say this and we run it.
So we have 30.
Now let's up different numbers.
Let's say one and five to 100 so we should have thirty six and one hundred two.
Let's see how this works great.
Thirty six hundred two.
Now as I told you before functions can return 0 or more values.
In this case some Jews return one value.
But if we want to return more than one let's write an example to show that.
So let's write our swap function swap will swap values.
So if it receives X and Y it will return y next.
Let's say X and Y are integers.
And in this case we open parentheses and we said that we're going to return an integer and an integer
so we receive X and Y but we return y coma x.
Okay.
So let me remove this.
Okay.
Now if we call swap of 10 and 20 How do we get those arguments.
We will do a and b are equal to swap 10 and 20.
So the value of a and the value of b are going to be defined by this function.
So what we're doing here is we're calling the swap function with 10 and 20.
And we know that the function is returning 20 and 10.
So a should be 20 and B should be 10.
So what this program will print is 20 which is the value of a here and 10 which is the value of B.
Now the last thing about functions is that you can define sums thing call closures closures are functions
assigned to variables.
Now we just saw in the previous chapter.
So let's say we want to define the sum function is gonna be local to the main function.
So to do it we use the syntax we saw it recently.
And we do it very similarly.
The fun the arguments x and y.
Which is going to be in time and the return type which is integer.
And now we define our function which is going to be return x plus y to call this.
It's exactly the same sum four and six.
Let's execute this and we'll have the same result.
So this is all four functions.
Just to summarize.
Remember that our name section of a program that performs a specific task they can receive zero more
arguments and they can return zero more values and they can be defined as closures.
Closures are very important for concurrency which we're gonna be looking into next chapters.
And that's a.