All right.
Now let's talk about function.
Functions are an important aspect in every programming language.
We have actually already been used in a function through our program, and that is the main function.
And remember, early on in this course, I said that the main function is the entry point to the program.
And as you can tell by the main function, we define a function by using the F in keyword.
So when we are creating a function, Russ is going to use what is known as snake casing as the conventional
style for function names.
Snake casing is all letters or lowercase and underscores are going to separate the words.
So an example of this would be this is snake casing.
So everything is lowercase and all the words are separated by underscores.
So let's create a simple function that will print out a phrase from our function.
So we're going to use the FN key word to signal the signal to rest.
Hey, we're about to create a function and we're going to call it print phase using our snake casing.
So following the name of our.
Function.
We are going to have an open and a closed parentheses and currently there's nothing in there.
And then we're going to have an open and a closed bracket, and that's going to basically signify the.
The scope of our function.
So everything inside of these open and closed brackets is going to belong to our function.
And in this case, our function is print phrase.
So now let's go ahead and create a print statement that is going to say hello from the function.
So now let's just go up here and to call our function.
All we have to do is.
Type in the name of our function and I'm going to get rid of the default hello world.
So all we have now is just saying hey print phrase, call our function, which is the open and close
parentheses.
That is how you say, Hey, I want to call my function so now we can run cargo run and we see we got
hello from the function.
So sweet.
We got our first function done.
Let's continue building on that.
So now inside these open and closed parentheses, we can add what's called arguments or parameters.
Arguments or parameters are just interchangeable words.
But inside of here, we can add in special variables that are part of the function signature.
So when a function has parameters, you can provide it with concrete values.
So let's create a variable inside of here and we'll call it phrase, and we're going to give it a string
slice.
Or a reference to a string.
So inside print phrase, we're going to say, hey, I want you to print.
Print my argument.
All righty.
So now we're passing in an argument.
So down here, all we have to do now is.
Print are variable.
Like we've always done, which in this case our variable is phrase.
So we're just creating our argument up here.
Passing it down into this phrase variable.
Through a string slice and saying, hey, I want you to print it out.
So now if we car go run it.
Success.
We have successfully printed out our argument of print my argument.
So now another functionality that functions has is we can return values from it.
So I want to I'm going to comment all this out and let's do a fun little function of the greatest common
denominator, a very well known little algorithm.
And what we'll do here is we'll give two values, and we are going to return the greatest common denominator
between those two values.
And inside this function, we're also going to get a look at some control flow statements, which is
what is going to be the topic of our next lecture.
So let's call it create our function, call it GCD, and we're going to create a mutable variable,
a.
And give it a value of unsigned 64 integer.
And then we're also going to create a mutable variable B and also give it an unsigned 64.
Now to return a variable or to return a value from a function, you use the little arrow, the type
that you want to return, which in this case is going to be unsigned 64.
And now we fill in our function with the logic.
So we're going to create a Y loop.
And while a does not equal to zero.
And then if A is less than B, and again, we will get into depth about what these control flow statements
are.
In the next lecture, I just want to show you kind of a little fun example of creating a function B
equals C.
Come down here.
A equals A remainder B.
And then we want to return B.
So now all of our ears that it was saying that we had are now gone.
So to return a value you put it at the.
You signify it by not including a semicolon so we can return values.
And as we continue to grow and continue our journey, you will see that you can have more than one return
value in a function.
But depending on your logic, only one value will be returned.
But you can have return values in multiple places.
And this case, it happened to be at the end of our function and we signify that it's a return value
by leaving off the semicolon at the end.
So now we'll go up here and we will say.
GCD.
We're going to pass in two parameters.
So for our first parameter, let's pass in 20.
And then for our second parameter, let's pass in five.
But we also want to print this out.
So let's just go ahead and put it inside of a print statement.
That way we can verify our work is correct.
And now let's run it.
So awesome.
We got the GCD of five, which is correct.
So now let's just to verify, let's change it to four and make sure that it's now four.
Awesome.
So we now have a working greatest common denominator function.
And since I went ahead and brought it up, I do want to just go ahead and show you how you can have
multiple return values.
And I want to pass in a.
Uh, I'll call it Flag.
I want it to be a boolean.
I want it to turn a boolean.
So in this case, if flag equals true.
I want to return.
True or else.
False.
So what I'm basically saying here is if the flag that I pass in is true, return true, let's return
false.
So we have to return values in here.
We have a true value that can be returned and a false value, and it's going to be dependent upon what
I pass in here.
So just to show this to you, let me go ahead and say I want you to print out
my multiple return value.
And in this case, we're going to print out true in this one cargo run.
So we have true.
And then if we come up here and say false, we now print it out false.
So as just an example of how you can print out multiple potential return values, it will be through
control flow statements which we will cover in the next lecture.