Functions
How Do They Work?
Functions in Scala
Calling a Function
Ordinary Method Call
Method Call Using Operator Notation
Functions
In computer programming, a function or a method is a block of code that performs a specific task. That block of code is then given a name (much like a variable) that is called whenever that specific task needs to be performed. This removes the need to type the same code over and over again; all you have to do is call the function’s name.
How Do They Work?
Like mathematical functions, programming functions take in an input, known as an argument, perform some operations on that input, and then return the resulting output.
It’s almost like a conveyer belt in a factory with items entering the machine from one end of the conveyer belt and coming out, completely modified, on the other end. However, as the machine remains the same throughout its life, it will modify every item that enters it the same way.