In this video, get a brief refresher of the following aspects of working with Kotlin: functions, expressions, and variables.
- [Narrator] Let's take a quick look at Kotlin Syntax, and we're going to start with functions. For this, we're inside of our functions file, and we're going to look at two scenarios: basic function creation, and execution. Then we'll also focus on how we can use default values in our function parameters. So the first thing we're going to do is come to the top of the file and declare a function. Now in Kotlin, you don't have to declare a function inside of a class. It can also be at the top level of a file, which is what we're going to do now.
So we're going to create a function, called 'multiply,' and we're going to start with the 'fun' keyword because this is how you declare a function. And then we're going to call it 'multiply.' And our function is going to take into parameters. The left upper Int, which is an integer, and then the right, which is also going to be an integer. Notice how the type of the variable comes after the name, and then a colon. Now, this function is going to return an integer as well, so we'll put another colon and then 'Int.' Now for our function definition, that's going to be placed inside of curly braces.
So we're just going to type 'return,' and then we'll do left x right. And that's it, we've defined our function. Now let's look at how we can use it. We're going to come down to the function creation method, and then we're just going to print out the result. So we'll use print line, and we'll make a call to our 'multiply' method, and we'll supply the numbers two and three. Just like that, we've now made a call to our function. So if we run this test, we'll see that it prints out the number six just as we had expected.
But, since Kotlin supports name parameters, we can also duplicate this line and we can explicitly name the number two as left, and the number three as right. Just like that. And if we run the test again, we'll get the same result, the number six. Perfect. Now let's look at our next scenario where we're going to use default values. Default values are just as the name implies. It's a way that you can specify a placeholder when nothing else is provided as input.
So if we come back up to our multiply method, we can set the right upper Int to a default value of one. So we just put the equal sign, and then the value that we want to use. So now we have a few options on how we can work with it. To test this out, let's highlight what we have inside of the function creation method, and then let's paste it down here inside of our function default values test. So the first thing you can do is just ignore the right parameter. So here on line 19, if I delete the number three, the call to the multiply method is still valid.
The same is true if I decide to remove the name parameter call here on line 20. So if we run this test, then we're going to get the value two. Because by default, right equals one. Now let's move over to our next file, Expressions.kt. Here, we're going to look at a few examples of what Kotlin considers to be valid expressions. And so when we say expressions, we mean any section of code that evaluates to a value.
So let's look at our first example, and that's going to be using 'if' as an expression. So for now, let's close this panel down, come back to the top of the file, and we're going to create a function called 'isEven.' And we're going to use this to determine whether or not a particular value is an even number. And it's going to return a string as the result. So we'll start with our return statement, and then we'll do our 'if' check. So if we can take that value, mod it with two, and it equals equals zero, this is the standard way to do checks for whether or not a number is even.
Then we're going to return the string, 'it's even.' 'Else,' we're going to return 'it's odd.' Okay, so just like that we're using 'if' as an expression. And this differs from Java because 'if' is only a statement there, and it doesn't resolve to a value. So coming down into our expressions test class, let's print out what we get when we call 'isEven' on the numbers two and three.
So first the number two, and then we'll duplicate this line here on 18, and we'll provide the number three. So let's come and run the test, and we see that we get our desired values. So two comes back as 'it's even,' and three comes back as 'it's odd.' Now let's look at one more example of a control structure that can be used as an expression in Kotlin. This time, we'll look at try/catch. So right underneath where we've created our 'isEven' function, of course let's close this pane down, we're going to add a new function called 'stringToInt.' And this function is going to take in a given string, which we'll call 'x,' and then it's going to return its integer representation.
But what we're going to do differently is we're going to rely on the try/catch block for this. So first we're going to try to directly convert the string to an integer using the 'toInt' function. And then, if it's not a valid string, meaning that it gives us a NumberFormatException, we're just going to return zero. So just like that, we have our function that uses try/catch as an expression. So if we come down here to our 'tryCatchAsExpression' test on line 31, we can go ahead and print out the results of calling our method first on the string which contains the number two, and then we'll duplicate this on the string that contains the word 'cake.' So let's run this test.
And as we expected, we get the number two, and then the number zero. Perfect. So let's move over to our final file, Variables.kt. Here, we're going to look at the two types of variables: readOnly, and then mutable. So let's look at our first example, readOnly, starting here on line nine. We're going to create a readOnly variable by using val, and we're going to name the variable 'flavor.' We'll set this flavor to vanilla.
Now notice what happens when we attempt to change the value of our flavor variable. So here on line 10, I'm going to try to set the flavor now equal to 'Mint.' And we receive an error because val cannot be reassigned. So the val is for an immutable reference. Now let's copy this same code, and paste it down here inside of our 'mutable' method. And this time, we're going to change 'val' to 'var,' and notice that the error goes away.
That's because var is used for mutable references, so the variable can be reassigned. By default, you should try to declare all variables in Kotlin with the val keyword. This makes your code easier to work with, and closer to functional-style programming. Now that's it for this brief overview. But if at any time you need a refresher on any particular aspect of the language, I'd recommend you go over to try.kotlinlang.org. They have simple examples that you can execute right there in your browser.
- What Kotlin has to offer
- Working with lambdas
- Common Android extension functions
- Kotlin Android Extensions overview
- Making use of the Anko library
- Working with coroutines
- Nullability and collections