Get immediate access to this and over 1,500+ other videos and books.

Boost your skills with a raywenderlich.com Beginner subscription. With over 60+ video courses and our core foundational programming books bundled in one subscription, it’s simply the best investment you can make in your development career.

Store a Function

This video was last updated on Jun 21 2022

One of the coolest parts of functions is storing them and passing them around like any other variable. This episode will get you started.

Contributors

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Functions are a great way to call code on demand. One convenient aspect of functions is that we can store them in variables and pass them around to other functions. In the second part of this course, you’ll see this in action. But for now, just know this is a great way to create dynamic and adaptable programs.

int multiply(int a, int b) {
    return a * b;
}
print(multiply(10, 10));
var myFunction = print();
var myFunction = multiply;
void main() {
    var scores = [54, 75, 32];
}
int processScores(List<int> scores, Function processor) {

}
int processScores(List<int> scores, Function processor) {
    var total = 0;

    return total;
}
for (var score in scores) {
    var number = processor(score, 2);
}
total += number;
total += (number is int) ? number : 0;
int processScores(List<int> scores, int Function(int, int) processor) {
for (var score in scores) {
    total += processor(score, 2);
}
void main() {
  var scores = [54, 75, 32];
  print(processScores(scores, multiply));
}

Reviews

Comments