From the course: Scala Essential Training for Data Science (2017)

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Scala functions

Scala functions

- [Instructor] Lets talk about scala functions. I've started the Scala REPL here. Now scala functions are expressions that can be called with parameters to compute a value. If you've worked with other programming languages that use functions, you've probably worked with a syntax that allows you to define a function, give it a name, and optionally pass in some parameters. We can do this in Scala as well. Let's look at the structure. We use the def keyword to define a function. Give it a name like myFunction, and tell it what parameters we want to pass in, like the variable a, which is a type integer, and b, which is also a type integer. Then I want to indicate that this function is going to return a particular value. In this case, it'll return an integer. And I want to compute a new value within this function called c, and c is going to be the value a times b. Then I simply want to return a value of c. Then I will close off my definition and now I have my function. Now once we've…

Contents