Function syntax
Functions in Scala can be written using the def
keyword, followed by the name of the function, with some arguments supplied as inputs to the function. Let's take a look at the generic syntax for a function:
modifiers... def function_name(arg1: arg1_type, arg2: arg2_type,...): return_type = ???
The preceding syntax shows the generic function signature in Scala. First, we give modifiers for the function. Modifiers can be understood as properties defined for the function. Modifiers come in different forms. A few of them are as follows:
- Annotations
- Override modifier
- Access modifiers (
private
, and so on) - The
final
keyword
It's recommended practice to use the preceding modifiers on an as-needed basis and in the given order. After specifying modifiers, we use the def
keyword to denote a function followed by the name of the function. After giving the function name, we specify the parameters. The parameters are specified in parenthesis: first, the name of the parameter and then its type...