Home Business Android Kotlin Functions and Visibility Modifiers – Syntax of the kotlin functions tutorial!

Android Kotlin Functions and Visibility Modifiers – Syntax of the kotlin functions tutorial!

by Admin

Within this android Kotlin article, we find out about the Kotlin Functions and Visibility Modifiers. Also, you can learn all you need to know regarding Kotlin functionality and visual modifiers.


The syntax of method in Java

In Java, to define the behaviour of objects, quite simply, to define tasks we employ methods. Such as this. The code in this is one particualr Java method designed to develop a String message using three parameters.


public String getMessage(int marks, String subject, double average)

The General Public modifier is definitely an access modifier. It’s also among the method’s returns formats. GetMessage may be the method’s name(identifier). Marks subjects, marks, and averages are three variables for that procedure.


Syntax of the kotlin functions tutorial

Within this class within this lesson, we’ll check out the different ways in Kotlin.In Kotlin language syntax from the technique is slightly not the same as the way a technique is written utilized in Java. Whenever we use Kotlin we don’t make use of the term methods. We call them Kotlin functions.


Allow me to demonstrate the syntax from the Kotlin function.

Visibility Modifiers fun function Name ( Listing of  parameters ) : Return type

The Visibility modifier may be the first. After that is there’s the ” fun ” keyword, that is mandatory. Then is”the name” from the program. A summary of parameters A colon. After which, the return type. Only then do we obtain the body from the method. Here’s a good example of Kotlin code that works with this syntax.


Visibility Modifiers fun function Name ( Listing of  parameters ) : Return type

private fun getSum(num1 : Int, num2 : Int) : Int

Within the kotlin function example above “private” may be the visibility modifier(we are discussing visibility modifiers soon). We’ve then added the term “fun”. The function’s name”getSum “getSum”. We’ve used two integer figures as parameters. Additionally, the return type is int. For Java in addition to Kotlin we have to use CamelCase the naming convention for methods names.


kotlin function example

private fun getSum(num1 : Int, num2 : Int) : Int

Okay, let’s locate a code example. I’ll complete exactly the same task as described in the last publish to acquire the merchandise of two figures.

fun getMessage( marks : Int,subject : String ,average : Double ) : String


Kotlin Visibility Modifiers

Whenever we used Java we’d access modifiers. Protected, private, and public. Much like Kolin you will find visibility modifications. We’ve private, public security, and internal.

It is because the conventional modification to visibility is openly visible. When we don’t specify any modifier then visibility is public. People from the public are available with other classes. Every class that sees the declaring class has the capacity to access its people.

Private modifier restricts accessibility file or class which contains the declaration. Which means that whenever you declare a technique or variable private, it are only visible inside the class that it belongs. When we classify a specific class to become private, it will likely be available to classes within this file.

internal modifies is really a module-level modifier. It is just visible within that very same module. Any module class that can access the declaring class can access its people inside the module Android Features.


Protected modifier sits dormant to find the best-level method declarations. The protected function is going to be purely available through the declaring class and subclasses.

  • We are public if we don’t use any modifiers the visibility is going to be public.
  • Private: Limits visibility towards the class or file
  • Internal: Only visible inside the module
  • Protected: Only visible within the class and its subclasses.


You may also like

Leave a Comment