0% found this document useful (0 votes)
4 views

CS31-AppDev_Module 4

This document provides an overview of functions in Dart, highlighting their importance for code organization and reuse. It covers various aspects of functions, including parameters, the main() function, and concepts like first-class objects, anonymous functions, lexical scope, closures, and tear-offs. The document aims to equip students with the knowledge to create and manipulate functions effectively.

Uploaded by

Xander Pardico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CS31-AppDev_Module 4

This document provides an overview of functions in Dart, highlighting their importance for code organization and reuse. It covers various aspects of functions, including parameters, the main() function, and concepts like first-class objects, anonymous functions, lexical scope, closures, and tear-offs. The document aims to equip students with the knowledge to create and manipulate functions effectively.

Uploaded by

Xander Pardico
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CS31-AppDev (Android Development and Emerging Technologies)

College of Computer Studies

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.

III. PRELIM INARY ACTIVITIES


N/A

Page 1 of 10
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies

IV. LESSON PROPER


A. Function
Dart is a true object-oriented language, so even functions are objects and have a type, Function. This
means that functions can be assigned to variables or passed as arguments to other functions.

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:

If you instead want a named parameter to be mandatory, requiring callers to provide a


value for the parameter, annotate them with required:

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:

b. Optional positional parameters


Wrapping a set of function parameters in [] marks them as optional positional parameters. If
you don't provide a default value, their types must be nullable as their default value will be null:

Here's an example of calling this function without the optional parameter:

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:

String say(String from, String msg, [String device = 'carrier pigeon']) {


var result = '$from says $msg with a $device';
return result;
}

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 a simple main() function:

Here's an example of the main() function for a command-line app that takes arguments:

D. Functions as first-class objects


You can pass a function as a parameter to another function. For example: This example we make use of
a built-in function “for each” and pass the prinElement user-defined function.

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.

An anonymous function resembles a named function as it has:

 Zero or more parameters, comma-separated

 Optional type annotations between parentheses.

The following code block contains the function's body:

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.

var uppercaseList = list.map((item) => item.toUpperCase()).toList();


uppercaseList.forEach((item) => print('$item: ${item.length}'));

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.

Example: A series of nested functions with variables at each scope level:

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

VI. ADDITIONAL RESOURCES

VII. ASSESSMENT

VIII. REFERENCES
https://dart.dev/language/functions

Page 10 of 10

You might also like