Welcome back.
In this video, we are going to look at functions and functions are something that we have used already,
for example, the main function, and now we're going to see how we can create our own functions, which
we then can use to reuse code very easily.
And in order to do so, I'm going to clean up this whole main function now because it's going to be
a lot clearer if we have everything on one screen.
So we have this main function.
And as you know, this is the entry point of our program and that's where everything begins.
So if no code isn't here, then nothing will happen if we start the program.
So that's important to know and you know that already.
So the next thing we can do is we can create code outside of this function and then call it within this
function.
And in order to do so, you can just go ahead and create a new function with the keyboard fun.
And then you give this a name and I'm going to call this one my function.
And this is generally the structure that you have, so you have this keyword fun, then you have a name
for the function, you have rounded brackets and then you have curly brackets.
And within the rounded brackets, you can put all your parameters within the curly brackets.
You can add to code block that should be executed once this function is called.
So what do all of these terms mean and what we will see in the second?
So let's get started first with something very basic.
So I'm just going to use print here and I'm just going to say called from my function.
All right.
So now let's just call this my function.
How do we call a function?
Well, we call it by just using the name of the function and then the brackets here.
And if we have any arguments that we want to pass, then we need to pass them as well.
So let's run this code just to see if this is working.
And once you run it, you will see that it's going to say called from my function.
All right, so this print statement that was within my function, so within the function that we called
my function was called once we called this function here.
So if we decided to call this function twice, then it's going to display code for my function twice
here and that we are called for my function and again called from my function.
And as we are using a print statement, they are dialed directly after another.
So there is no space in between or no line break either.
So this is our very basic function called my function.
Now we can extend functions and in order to do so, I'm going to create a new function so we can make
functions a lot more complex.
For example, I can call one add up and I'm just going to have two variables here.
I'm going to have one variable which is of type it and another variable, which is also type int and
it should return a variable of type int.
So what do all of these things do?
Well, first of all, we have two parameters here.
So this is parameter one, which is called a which is of type integer.
And then we have another parameter, which is called B and it's also of type integer.
So now the keyword here is parameter.
All right.
So we have two parameters here.
Then after the colon, we say of which type the return type should be.
So in this case, our function add up is going to return something once we call it.
So instead of just executing some code in there, it's also going to return something.
So it's going to give us something back that we can then reuse.
OK, so the idea here is that we need to use the return key word.
As you can see, it's complaining here.
So now it's complaining about the returns.
So let's say I always return zero, then this add up would always return zero no matter what I put into
it.
So let's change that a little bit.
Let's say I want to actually add up those two values so A and B. So what we will do is we would return
A plus B in this case because we want to return whatever A and B are added up.
Now it's going to return whatever is the result of A plus B, so our parameters is the definition of
the input that we want to have.
And then we have an output and it's also of type int.
So whatever will return will be our output and it should be of the type that we have defined here.
Parameter input and then return type is our output.
OK, so now we can go ahead and use this function, add up in our main function, so I'm going to get
rid of my function calls here and I'm just going to call add up and I'm going to add up with five and
three.
So this should return eight.
Now, the thing is, it will return eight, but we have no way of knowing that it returns eight except
for, let's say we store it in a variable or we use our debugging tools.
But I'm just going to store it in a variable, which I'm going to call result, and it's going to be
the result of whatever add up is going to come up with and it's going to store it in result.
So now what I'm going to do is I'm just going to use result is and concatenation year result.
So very basic.
And of course we're getting.
The template requests here.
So this is the alternative way.
All right, so result is result and now we can go ahead and test what we're actually going to get.
So there we are.
It says, results aside, as you can see, it worked.
So the input was five and three and the output was eight.
And that is what we saw as a result.
Now, there is something to say about the terms here because here.
What we are using is called parameters, right, and so we are using parameter and parameter B now when
we pass a value to our function called add up.
This is an argument and this is also an argument.
So the term here is argument.
All right.
You're not having an argument here, but we are using arguments.
OK, so the idea is that they are two different terms and this is something that people mix up all the
time.
And yeah, in some cases they really just ignore the whole concept of parmeter an argument or distinguishing
between the two.
So either Parmeter or argument is going to be fine when you use it in combination with functions.
OK, but be aware that there is a difference between the term argument and parameter.
So I might even mix them the term terms sometimes when talking about function.
So please be forgiving then.
There is another thing about functions and you probably heard the term or maybe heard the term methods.
So there is this called method and a method is a function within a class.
So that's the only difference between the two.
Otherwise they are the same.
So in some programming languages, which are not object oriented programming languages, then it's always
going to be a function.
But in programming languages which are object oriented, it's going to be most of the case as a method
because it is defined within a class in our case here.
However, we are not having a class, so it's not like in our main activity, for example, in our class
main activity there we have a function called uncreate.
But to be more precise, this is a method because it is within a class.
So it is a method here.
There are a bunch of other words that come up in programming languages.
Right.
And what it really helps is a dictionary sometimes.
So Cutline program, a dictionary, you can find it under plug that cutline minus academy dot com.
So they wrote a little dictionary with the main words here.
So function versus procedure, then function versus method and so forth.
So here are a bunch more field versus property.
What's the difference here?
Parameter versus argument.
That's something that we have seen before and so forth.
So if you want to really dig deep into all of the different terms and terminology, then check out this
article here.
Already, so coming back to our cutline basics, a little challenge for you, please write another function
which will not add up, but which will build the average of two values.
So here you have to be careful with the data type.
And yeah, just please try to build that function by yourself.
All right, so let's go ahead.
I hope you tried it so here I'm just going to use the funky word and then I'm going to call this one
a Viji standing for average.
And I need two values.
One of them will be a double and the other one should also be a double.
So the thing about those is that the return of an average usually is not a whole number.
So that's why I'm using doubles here.
And it's going to be a double as a return type as well here.
And then we can return A plus B divided by two.
That's going to be it.
So now, of course, we can see what the result is, so let's call this one average instead, and we're
going to not call the add up function, but we're going to call the average function.
And I'm just going to say, OK, what's the average of five and thirteen?
And then.
In order to be precise, we need to make those doubles so five point three and thirteen point three
seven and no, let's run this and we are as a result is nine point three, three, four or five.
All righty, so these are the basics of functions.
So what's the whole point of functions?
Well, there are multiple advantages of functions.
So one of them is that you can reuse code.
Another one is, for example, that you can work with a partner.
So you take care of one piece of code and he takes care of another piece of code or she takes care of
it.
And then you can just combine them by having functions that you can then reuse so you can write your
own functions and then you can both use the functions of the other person.
And then another thing is that you can use functions that are pre-built and you can just use the functionality
of them.
So that's the whole idea of functions.
They add functionality to your program and you can simply use them for whatever purpose you built them
or for whatever purpose they were built.
And there are gazillions of functions out there that you can reuse just by using this important feature
here, as you can see here, import Android OS bundle.
Well, in there there are a bunch of functions that we can use.
So if you open up the bundle class, for example, you can see that in there.
You have.
A bunch of.
Constructor's, but then we have a maybe Prefill has left the Earth function, then we have another
function called.
For pair, which is going to return a bundle.
Well, you might wonder what all of these public static bundle keywords mean.
Well, the thing is, this is Java code.
So this is not codling code.
And in Java, the structure of a function is slightly different than it is in Scotland.
All right.
But the basic idea is the same.
It's has a return type, then it has a name and then it has a bunch of parameters here.
Just the order is a little different.
But in the end, what it does, it has a bunch of code that you can then reuse.
So no worries if you don't understand the stuff that is going on in here, because for one, it's not
codling code.
And the second thing is you don't need to understand everything behind the function in order to use
the function.
Sometimes you just need to have the output.
Right.
And that is really powerful.
It's like driving a car.
You don't have to be a mechanic in order to drive a car or you don't need to be able to repair a car
in order to drive a car.
You can just sit in there and be happy about all of the features that were added to the car and that
you can now use.
All right.
So these are the basics of functions.
And, yeah, let's check out the next video where we're going to look at knuckleballs.
So see you there.