The course is part of this learning path
This course explores functions in Kotlin. You'll learn what a function is and how you create a function. We will then learn about declarations and calling of functions. Finally, we will look at function types and function return types.
This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.
This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.
Well, hello everyone. So, in this section we're going to introduce functions in Kotlin. So, first we'll learn what is a function and how do you create a function in Kotlin? We will then learn declaration and calling of functions. Then we will learn function types. And then we will learn function return types. So, a function is a set of statements to perform an operation. Functions are also known as procedures, as well as functions. The main advantage of function is code reusability. So, you can write a function once and then use it multiple times. You do not have to rewrite the entire code each time. So, you can think of it like, write once, reuse many. I don't know, I thought it was going to be cool but, one too many, once many. Once you write it, you can use it a lot.
So, you see the general form of a function declaration right here. A return type is a data type of the value that is returned by the function. It can be a primitive types such as Int, a class type such as string or unit. Each function has its own name. So, function name begins with a lowercase letter and compound words. So, in multi-word names, the first letter of each of the second and following words need to be capitalized. Now, we're going to discuss our naming rules for functions in the coming lectures, but for now, just park that in the back of your head. A parameter is a value passed to the function.
Functions can take one or more parameters. So, if the function has more than one parameter, then these parameters are separated by commas. If functions work with data, we must pass the data to the functions, and this is done by specifying them inside of parenthesis. If the function does not take any argument, simply leave the paranthesis empty. If any function is not declared unit, then it must contain a return statement with a corresponding return value. The data type of the return value must match the functions declared return type. So, you can't return an integer value from a function declared to return a Boolean. That makes sense. But don't worry, we'll cover function return types in much detail, coming up.
So, the function body, which is enclosed between braces. The function body includes the declaration of variables and some other codes of functions. Now finally, in our example, we define the function that its name is addNumber with the integer data type and we have x and y parameters. Our function finds the sum of x and y and saves a result in a new variable sum, and then returns the value of that variable sum. Also, we can directly return the x + y statement instead of assigning a new variable. So my friends, this is the general structure of functions in Kotlin programming language.
Now of course, we can use this function and we create it wherever we want. If we do not use it, the operations that we defined inside the function will not occur. So, in other words, we have a function but the function cannot perform its functions. So, how can we call the function that we create it? Well, that's what we're going to find out right now. So, a Kotlin function is called according to its functionality. It is called in any of these two situations: The first one when it completes all the statements, and then the second one when it reaches a return statement. So, in this example, we'll define a variable result with the int data type. Its initial value is zero. We call the addNumber function with arguments 10 and 20.
Argument values are passed to the parameter. Arithmetic sum is the operation that gets performed in the addNumber function and the return value of addNumber function is assigned to variable result. And now we show the result value 30 on the screen. Also, we can directly use the addNumber function with the printIn function instead of assigning a new variable. But as you see, the result is the same. All right, my friends, that's in general how you use functions in Kotlin. Now, don't worry though, we're going to go over a lot because we're going to reinforce this subject by doing many, many examples in a little bit. But first, I bet you want to learn the types of functions. So, we're going to take a short break here and I will see you in the next video.
Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.