Functions and Records
Functions
Typed Return
A function declares its parameter and return types. return exits with a value.
Program
Play the program to add tax to a subtotal.
functions.dart
Replay: real traced execution (multi-file project)
int addTax(int price) {
return price + price ~/ 10;
}
void main() {
var subtotal = 25;
var total = addTax(subtotal);
print(total);
}
subtotal ← 25
5void main() {6 var subtotal = 25;7 var total = addTax(subtotal);values this step25subtotalcall ← addTax(25)
6var subtotal = 25;7var total = addTax(subtotal);8print(total);values this stepaddTax(25)call25subtotalreturn value ← 27
1int addTax(int price) {2 return price + price ~/ 10;3}values this step27return value25pricetotal ← 27
6var subtotal = 25;7var total = addTax(subtotal);8print(total);values this step27totalprint(total);
7 var total = addTax(subtotal);8 print(total);9}output27values this step27total
typed signature
`int addTax(int price)` declares parameter and return types.
return
`return` exits the function with a value.
integer division
`price ~/ 10` is integer division for whole-cent rounding.