kotlin functions tutorial

Android Kotlin Functions and Visibility Modifiers


In this android Kotlin article, we learn about the Kotlin Functions and Visibility Modifiers. Also, you’ll be able to learn everything you need to know regarding Kotlin functionality and visible modifiers.

The syntax of method in Java

In Java, to define the behavior of objects, in other words, to define tasks we employ methods. Similar to this. The code in the following is an example of a Java method designed to generate a String message using three parameters.

public String getMessage(int marks, String subject, double average){
        return "Shivam got "+marks+" for "+subject+".average Markes "+average;

The Public modifier is an access modifier. It is also one of the method’s returns formats. GetMessage is the method’s name(identifier). Marks subjects, marks, and averages are three variables for the procedure.

Syntax of a kotlin functions tutorial

In this class in this lesson, we will examine the different methods in Kotlin.In Kotlin language syntax of the method is slightly different from how a method is written used in Java. When we use Kotlin we don’t use the term methods. We refer to them as Kotlin functions.

Let me demonstrate the syntax of the Kotlin function.

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


The Visibility modifier is the first. Following that is there is the ” fun ” keyword, which is mandatory. Then is”the name” of the program. A list of parameters A colon. And then, the return type. Then we get the body of the method. Here is an example of Kotlin code that is compatible with this syntax.

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

private fun getSum(num1 : Int, num2 : Int) : Int {
        return num1+ num2


In the kotlin function example above “private” is the visibility modifier(we will be discussing visibility modifiers in the near future). We have then added the word “fun”. The function’s name”getSum “getSum”. We have used two integer numbers as parameters. In addition, the return type is int. For both Java as well as Kotlin we must use CamelCase the naming convention for methods names.

kotlin function example

private fun getSum(num1 : Int, num2 : Int) : Int {
        return num1+ num2


Okay, let’s look for a code example. I will complete the same task as described in the previous post in return for the product of two figures.

fun getMessage( marks : Int,subject : String ,average : Double ) : String {
        return "I got $marks for $subject .My average was $average"


Kotlin Visibility Modifiers

When we used Java we had access modifiers. Protected, private, and public. Similar to Kolin there are visibility modifications. We have private, public security, and internal.

This is because the standard modification to visibility is publicly visible. If we don’t specify any modifier then visibility is public. Members of the public are accessible to other classes. Every class that sees the declaring class is able to access its members.

Private modifier restricts access to the file or class that contains the declaration. This means that when you declare a method or variable private, it will only be visible within the class to which it belongs. In the event that we classify a particular class to be private, it will be accessible to classes in this file.

internal modifies is a module-level modifier. It is only visible within that same module. Any module class that has access to the declaring class has access to its members within the module Android Features.

Protected modifier is not used for top-level method declarations. The protected function will be only accessible by the declaring class and subclasses.

  • We are public if we do not use any modifiers the visibility will be public.
  • Private: Limits visibility to the class or file
  • Internal: Only visible within the module
  • Protected: Only visible within the class and its subclasses.