CS31-AppDev_Module 4
CS31-AppDev_Module 4
I. INTRODUCTION
Functions are a fundamental part of Dart,
allowing you to encapsulate code for reuse and
better organization. Dart supports a variety of
function types and provides powerful features to
make functions flexible and expressive.
Module 4
II. OBJECTIVES
Dart Functions 1. Analyze the concept of functions in
Dart, parameters, and return
keyword.
2. The student will be able to create
functions.
3. Create, manipulate, and traverse the
collections.
Page 1 of 10
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
Although Effective Dart recommends type annotations for public APIs, the function still works if you
omit the types:
For functions that contain just one expression, you can use a shorthand syntax:
The => expr syntax is a shorthand for {return expr;}. The => notation is sometimes referred to
as arrow syntax.
Note:
Only expressions can appear between the arrow (=>) and the semicolon (;). Expressions evaluate to
values. This means that you can't write a statement where Dart expects a value. For example, you could
use a conditional expression but not an if statement.
In the previous example, _nobleGases[atomicNumber] != null; returns a boolean value. The function
then returns a boolean value that indicates whether the atomicNumber falls into the noble gas range.
Page 2 of 10
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
B. Parameters
A Function can have any number of required positional parameters. These can be followed either
by named parameters or by optional positional parameters (but not both).
You can use trailing commas when you pass arguments to a function or when you define function
parameters.
a. Named parameters
Named parameters are optional unless they're explicitly marked as required.
When defining a function, use {param1, param2, …} to specify named parameters. If you
don't provide a default value or mark a named parameter as required, their types must be
nullable as their default value will be null:
When calling a function, you can specify named arguments using paramName: value. For
example:
To define a default value for a named parameter besides null, use = to specify a default
value. The specified value must be a compile-time constant. For example:
Page 3 of 10
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
You might want to place positional arguments first, but Dart doesn't require it. Dart allows named
arguments to be placed anywhere in the argument list when it suits your API:
And here's an example of calling this function with the third parameter:
To define a default value for an optional positional parameter besides null, use = to specify a
default value. The specified value must be a compile-time constant. For example:
Page 4 of 10
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
assert(say('Bob', 'Howdy') == 'Bob says Howdy with a carrier pigeon');
C. The main() function
Every app must have a top-level main() function, which serves as the entry point to the app.
The main() function returns void and has an optional List<String> parameter for arguments.
Here's an example of the main() function for a command-line app that takes arguments:
Page 5 of 10
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
You can also assign a function to a variable, such as: This example we used an anonymous function.
E. Anonymous functions
Though you name most functions, such as main() or printElement(). you can also create functions
without names. These functions are called anonymous functions, lambdas, or closures.
The following example defines an anonymous function with an untyped parameter, item. The
anonymous function passes it to the map function. The map function, invoked for each item in the list,
converts each string to uppercase. Then, the anonymous function passed to forEach, prints each converted
string with its length.
If the function contains only a single expression or return statement, you can shorten it using arrow
notation.
Page 6 of 10
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
F. Lexical Scope
Dart determines the scope of variables based on the layout of its code. A programming language with
this feature is termed a lexically scoped language. You can "follow the curly braces outwards" to see if a
variable is in scope.
The nestedFunction() method can use variables from every level, all the way up to the top level.
Page 7 of 10
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
G. Closure
A function object that can access variables in its lexical scope when the function sits outside that scope
is called a closure.
Functions can close over variables defined in surrounding scopes. In the following
example, makeAdder() captures the variable addBy. Wherever the returned function goes, it
remembers addBy.
H. Tear-offs
When you refer to a function, method, or named constructor without parentheses, Dart creates a tear-off.
This is a closure that takes the same parameters as the function and invokes the underlying function when
you call it. If your code needs a closure that invokes a named function with the same parameters as the
closure accepts, don't wrap the call in a lambda. Use a tear-off.
Page 8 of 10
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
Page 9 of 10
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
V. PRACTICE EXERCISES/ACTIVITIES
N/A
VII. ASSESSMENT
VIII. REFERENCES
https://dart.dev/language/functions
Page 10 of 10