Functions
Learning objectives
- You practice the basics of programming with Dart.
- You know the basics of working with functions with Dart.
Defining functions
Functions in Dart have a return type, optional parameters, and a function body. In addition, most of the functions have a name. In the following example, we define a function called main
that has void
as the return type, no parameters, and an empty function body.
void main() {
}
The return type void
states that the function does not return a value. The name of the function -- here main
-- is followed by parentheses ()
, which would include parameters if there were any. The function body is defined as a block that starts with opening curly brackets {
and ends with closing curly brackets }
. The block comes after the parentheses.
Parameters
Function parameters are used to pass values to the function. The parameters can then be used within the function. Parameters are added to the parentheses in the function definition. If there are multiple parameters, then the parameters are separated by commas. When programming using Dart, the convention is to specify the types for the function parameters.
Specify types of the function parameters when programming with Dart.
The following example defines a function called printSum
that takes two integers as parameters, calculates their sum, and then prints it. The function has void
as the return type, which means that the function does not return a value. The function is called from the function main
.
Name variables and functions using lowerCamelCase
Dart has a few naming conventions. One of these is:
Class members, top-level definitions, variables, parameters, and named parameters should capitalize the first letter of each word except the first word, and use no separators.
Source: Effective Dart: Style
Thus, we name variables and functions using lowerCamelCase
.
The following example defines a function called bondGreeting
, which takes two strings as parameters, and prints a classic "James Bond"-like greeting using the parameters.
Pass-by-value
Dart is pass-by-value. In the context of functions, this means that whenever a function is called, the value of the argument used in calling the function is copied as a value to the parameter in the function.
Because of this, the following program outputs 1984
.
Like in other object-oriented programming languages, the value can be a reference though. We'll take a peek at this a bit later.
Returning values
Values are returned from functions using the return
keyword that is followed by the value that will be returned. When defining functions that return a value, we also define the type of the value that the function will return. The return type is placed before the name of the function.
In the following example, we define a function called average
that takes two integers as parameters and returns their average as a double.
Similarly, the following function takes two strings (first name and last name) as parameters, and returns a James Bond -like greeting as a string.
Arrow syntax
Dart supports the arrow syntax for defining functions. In practice, the arrow syntax is used to define a function that executes the code followed by the arrow. As an example, the following program would print "Hello world!".
Functions defined using the arrow syntax return the value of the statement that is on the right hand side of the arrow. The following example defines a function called sum
that takes two integers as parameters and returns their sum.
Functions within functions
Functions can also be defined within functions. In the following example, we define a function called printHello
within the function main
. We then proceed to call the function and finally print its type.
Functions are objects
Functions are objects and they can also be passed as function parameters. In the following example, within main
, we define a function printWorld
. The function is given as a parameter to the function printAndExecute
that prints the string that it is given as the first parameter and then calls the function that it is given as the second parameter.
Anonymous functions
The previous example could be also written using an anonymous function, where the function that is given as a parameter to the printAndExecute
function does not have an explicit name.