Menu
CoddyTech

Functions That Return Values

Part of the Making It Interactive section of Coddy's Flutter journey. Lesson 8 of 34.

Some functions work out an answer and hand it back. Instead of void, they start with the type of the answer, and return gives it back:

int twice(int n) {
  return n * 2;
}

Text('${twice(21)}')   // 42

A call to twice(21) stands for its answer, so you can use it anywhere a value goes: in a variable, in a calculation, or inside a string with ${ }.

When the whole function is one return, Dart has a short form with an arrow =>. These two mean the same thing:

int twice(int n) { return n * 2; }
int twice(int n) => n * 2;

You will see the arrow form in the next chapter.

challenge icon

Challenge

Easy

Write a function minutes that takes a number of hours as an int and returns how many minutes that is (hours times 60). Use it so the two lines say 2 hours is 120 minutes and 5 hours is 300 minutes.

Try it yourself

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text('2 hours is ? minutes'),
              Text('5 hours is ? minutes'),
            ],
          ),
        ),
      ),
    );
  }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Making It Interactive

Practice on your own: Online Flutter compiler