Menu
Coddy logo textTech

Methods

Part of the Object Oriented Programming section of Coddy's Dart journey — lesson 6 of 110.

Methods are functions that belong to a class. They define the behaviors or actions that objects can perform.

Here is an example of a class with methods:

class Calculator {
  void greet() {
    print('Hello! I am a calculator.');
  }

  int add(int a, int b) {
    return a + b;
  }

  int multiply(int x, int y) {
    int result = x * y;
    print('$x x $y = $result');
    return result;
  }
}

Method signatures in Dart always include a return type. Use void when the method returns nothing.

Create a calculator object:

Calculator myCalc = Calculator();

Call a method that needs no parameters:

myCalc.greet();

Call methods with parameters and capture the return value:

int sumResult = myCalc.add(5, 3);
print(sumResult);

Call a method that both prints and returns a value:

int product = myCalc.multiply(4, 7);

Output:

Hello! I am a calculator.
8
4 x 7 = 28

Methods can:

  • Have a return type like int add(int a, int b)
  • Return values using return
  • Print output directly using print()
  • Be void when they do not return anything

Key Point: Call methods on objects using the dot operator: object.methodName(args).

challenge icon

Challenge

Easy

Create a BankAccount class in bank_account.dart with:

  1. A double balance instance variable set to 0
  2. A deposit method that takes a double amount and adds it to balance
  3. A withdraw method that takes a double amount and subtracts it from balance
  4. A getBalance method that returns the current balance

Then in driver.dart, create an account, deposit 100, withdraw 30, and print the balance in the format: 'Current balance: $[balance]'

Cheat sheet

Methods are functions that belong to a class and define behaviors that objects can perform.

Define methods with a return type. Use void when the method returns nothing:

class Calculator {
  void greet() {
    print('Hello! I am a calculator.');
  }

  int add(int a, int b) {
    return a + b;
  }

  int multiply(int x, int y) {
    int result = x * y;
    print('$x x $y = $result');
    return result;
  }
}

Call methods on objects using the dot operator:

Calculator myCalc = Calculator();
myCalc.greet();  // No parameters

int sumResult = myCalc.add(5, 3);  // With parameters, capture return value
print(sumResult);

int product = myCalc.multiply(4, 7);  // Prints and returns

Methods can have return types, return values using return, print output directly using print(), or be void when they don't return anything.

Try it yourself

import 'bank_account.dart';

void main() {
  BankAccount myAccount = BankAccount();
  myAccount.balance = 0;

  // TODO: Deposit 100

  // TODO: Withdraw 30

  // TODO: Print balance in format: 'Current balance: $[balance]'
  // Hint: use .toInt() on the balance for clean output
}
quiz iconTest yourself

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

All lessons in Object Oriented Programming