Conteúdo do Curso
Android Development with Kotlin
Android Development with Kotlin
Using Functions
To better understand and grasp functions, this chapter will look at several interesting examples that use different parameters for the function and return various types of data from the function.
Default Parameter Values
Let's look at an example of a function where parameters can have default values.
The following function will take two parameters: the first is what dish the customer ordered in the restaurant, and the second is whether the customer has any allergies and what they are:
Main
fun orderDetails(dish: String, allergies: String = "No allergies") { println("The customer ordered the $dish, he has $allergies") } fun main() { val firstDishName: String = "Pasta" val secondDishName: String = "Pizza" val secondCustomerAllergies: String = "Lactose intolerance" //for the first order the method will look like this: orderDetails(firstDishName) //for the second order the method will look like this: orderDetails(secondDishName, secondCustomerAllergies) }
If you run this code, you will see the correct output. Also, if we don't specify the second parameter's value when calling the function, it is replaced by the default value defined in the function body.
Note
These default parameters will be very useful for you when you get different values in a function from the API, and some of them will remain default without throwing exceptions.
Nested and Local Functions
Functions can be declared inside other functions. For example, as seen in the example, the outer function will print one text, and the inner function will print a completely different text:
Main
fun outerFunction() { println("This is the outer function") fun innerFunction() { println("This is the inner function") } innerFunction() } fun main() { outerFunction() }
As you can see, both lines specified in the outer and inner functions were printed.
However, there are some points to note when using inner functions:
- Variables and fields from the outer function are accessible to the inner function;
- Variables and functions from the inner function are not accessible to the outer function;
- The inner function cannot be called outside the outer function.
Let's look at an example where we will get an error when we try to break these rules:
Main
fun outerFunction() { val outerVariable = "I'm an outer variable" fun innerFunction() { val innerVariable = "I'm an inner variable" println(outerVariable) // A nested function can see the variables of the parent function println(innerVariable) } innerFunction() println(innerVariable) // Error: innerVariable is not available in this scope } fun main() { outerFunction() }
When calling the outer function, we see an error because we attempt to call a function outside its scope.
Let's delve deeper into scope in the context of functions.
Scope
It's important to understand that declaring variables within one scope and attempting to call these variables outside of that scope will result in an error.
This happens due to scope, so let's discuss a few rules.
Variables declared within one function can only be used within that function where they were declared. These variables are called local variables.
Attempting to use local variables outside the scope where they were declared will result in Kotlin warning us that it won't work, and running such code will give us an error.
The same applies to functions: a function declared within the body of another function can access the fields and variables of the outer function because it's also part of the outer function.
This will become second nature with practice; for now, just remember this small piece of theory.
Summary
Functions are incredibly powerful tool that allow for well-structured code and optimal results. Kotlin revolves around functions, so it's crucial to understand them!
As we continue through the course, we will keep using functions, transferring all code into various functions.
Obrigado pelo seu feedback!