...

/

Defining a Function

Defining a Function

In this lesson, we will start our discussion on functions and learn how to define our very own function.

Writing Your First Function

In a previous lesson, you were introduced to built-in functions; also known as methods. In this chapter, we will cover user-defined functions and learn how to write our very own functions. Let’s get straight to it and look at an example of a user-defined function.

def sum(x: Double, y: Double): Double ={
x+y
}

The above function takes two integers and returns their sum. Let’s look at what is exactly happening in the code above.

Syntax

  • def is the keyword used for defining a function the same way val and var are used for ...