Summary: in this tutorial, you’ll learn about Dart functions and how to define reusable functions.
Introduction to the Dart functions
A function is a reusable unit of code that performs a task.
In programming, you often need to perform the same task multiple times. And you don’t want to copy & paste the same code all over places in the program.
To do this, you wrap the code in a function and use it instead of copying/pasting the same code.
So far, you learned how to use the
function to display a value to the console. When you call the print()
function, Dart executes the code inside the print()
function internally.print()
It’s a good practice to use functions to divide a program into smaller and more manageable parts. Each function is small so that you can it can be easier to build, test, and maintain.
Define a function
The following defines a function that displays a greeting message:
void greet() {
print('Hi there');
}
Code language: Dart (dart)
A function has two parts:
- Function signature
- Function body
1) Function signature
A function signature has a return type, function name, and parameter list.
The return type specifies the type of value that the function returns. If a function doesn’t return anything, you can use the void as the return type.
For example, the greet()
function doesn’t return any value, it uses void as the return type.
Typically, a function has a name. By convention, you should follow the lowerCamelCase naming convention.
Parameters are the inputs of the function. If a function has parameters, you need to specify it inside the parentheses ().
Since the greet()
function doesn’t have any parameter so there’s nothing inside the parentheses.
2) Function body
The code surrounded by the curly braces ({}
) that follows the function name is the function body. The function body consists of one or more statements that perform a specified task.
The greet()
function body uses the print()
function to display a message to the console.
Calling a function
Once you define a function, you can call it. When you call a function, Dart will execute the code inside the function.
To call a function, you specify the function name and provide the parameter if required in parentheses. For example, the following calls the greet()
function:
void greet() {
print('Hi there');
}
void main() {
greet();
}
Code language: Dart (dart)
Output:
Hi there
Code language: Dart (dart)
Passing information to Dart functions
If you want to greet users by name, you can specify a parameter in the greet()
function like this:
void greet(String name) {
print('Hi $name!');
}
Code language: Dart (dart)
The name
is a parameter of the greet()
function. Inside the function, you can use the name
parameter as a variable.
Note that you can access the name
parameter only within the body of the greet()
function, not the outside.
When calling a function with a parameter, you need to pass a value that corresponds to the parameter. For example:
void greet(String name) {
print('Hi $name!');
}
void main() {
greet('John');
}
Code language: Dart (dart)
Output:
Hi John!
Code language: Dart (dart)
The value that you pass into a function is called an argument. In the above example, the string literal 'John'
is an argument.
In addition, you can pass a variable to the function when calling it like this:
void greet(String name) {
print('Hi $name!');
}
void main() {
String name = 'Jane';
greet(name);
}
Code language: Dart (dart)
In this example, the name
variable is an argument of the greet()
function.
Parameters vs. Arguments
Sometimes, parameters and arguments are used interchangeably. In fact, they’re different. And it’s important to distinguish between them.
A parameter is a variable that you specify in the function signature when you define the function. For example, the greet()
function has a parameter called name
.
An argument is a value or variable that you pass into the function when calling it. For example, the string literal 'John'
is the function argument.
Returning a value
A function may return a value. To return a value from a function, you use the return
statement:
return value;
Code language: Dart (dart)
The following example changes the greet()
function to return a greeting instead of showing it to the console:
String greet(String name) {
return 'Hi $name!';
}
Code language: Dart (dart)
In this example, we change the void
to String
to indicate that the greet()
function will return a string. Also, inside the function body, we use the return
statement to return a string.
When calling the greet()
function, you can assign the return value to a variable like this:
String greet(String name) {
return 'Hi $name!';
}
void main() {
String name = 'Jane';
String greeting = greet(name);
print(greeting);
}
Code language: Dart (dart)
The new version greet()
function is more reusable than the previous one because it is not dependent on the print()
function.
Later, you can reuse the greet()
function across desktop, mobile, and web applications.
Since the print()
function accepts a string, you can call the greet()
function and immediately pass the return value to the print function like this:
String greet(String name) {
return 'Hi $name!';
}
void main() {
String name = 'Jane';
print(greet(name));
}
Code language: Dart (dart)
Return type inference
By looking at the return
statement, Dart can infer the type of the return value. Therefore, you don’t need to explicitly specify the return type as follows:
greet(String name, String title) {
return 'Hello $title $name!';
}
void main() {
print(greet('Jane', 'Professor'));
}
Code language: Dart (dart)
In this example, the greet()
function returns a string but doesn’t have an explicit return type.
Dart functions with multiple parameters
A function can have zero, one, or multiple parameters. The following adds one more parameter to the greet()
function:
String greet(String name, String title) {
return 'Hello $title $name!';
}
void main() {
print(greet('Jane', 'Professor'));
}
Code language: Dart (dart)
Output:
Hello Professor Jane!
Code language: Dart (dart)
In this example, the greet()
function has two parameters name
and title
. To separate between two parameters, you use a comma (,
).
When calling the function, you need to pass the exact number of the arguments that correspond to the parameters. Otherwise, you’ll get an error.
Summary
- A function is a reusable unit of code that performs one task and may return a value.
- A function consists of function signature and body.
- A function can have zero or more parameters.
- Use the
void
keyword as the return type if the function doesn’t return any value. - Use the
return
statement to return a value from a function.