Functions and Methods — GDScript

Skill codes on this page

Code Level Skill
C641 7–8 functions
C642 7–8 methods

The distinction that matters for VCE: a function is a named, reusable block of code; a method is a function that belongs to a class, called on an object. In GDScript both use func — what earns C642 over C641 is the object it lives on.

C641 — Functions

Definitions.

  • Function: a sequence of related code that has been given a name that can be called from other points in the source code.

  • Arguments / parameters: specific inputs passed into a function that act as local, temporary variables.

  • Return value: a value passed back to the caller, often assigned to a variable or tested in a condition.

# C641 — function: named, takes parameters, returns a value
func calculate_fare(zone: int, is_concession: bool) -> float:
    var fare: float = FARE_TABLE[zone]
    if is_concession:
        fare *= 0.5
    return fare
# C641 — function call: result assigned and used
var fare: float = calculate_fare(2, true)

Earns the tick: a function with parameters and a return value, called from elsewhere in the program. A func that Godot calls for you (_ready, _process) shows the syntax but not the skill — write and call your own.

In connect-go-dots (grid.gd): _open(photo: Texture2D) is a function you would label — typed parameter, hooked to every photo button. _ready() is not — Godot calls it. _open has no return value, so pair it with a returning function (like calculate_fare above) for the full tick.

In tile-matching-game (deck.gd → game.gd) — the full tick in real code: typed parameter, typed return, result used:

# tile-matching-game — deck.gd
static func load_csv(path: String) -> Array:   # C641 — typed parameter and return
    var cards: Array = []
    # … one Dictionary per CSV row …
    return cards
# tile-matching-game — game.gd
var all_cards := Deck.load_csv(deck_path)   # C641 — call: the result drives the board

Say the types out loud in validation: parameters and return are typed (zone: int, -> float) — that evidence also feeds C628.

C642 — Methods

Definition. An action an object can carry out (e.g. window.refresh, golfClub.swing).

class_name MykiCard
extends RefCounted

var _balance: float = 20.00

# C642 — method: an action the MykiCard object can carry out
func top_up(amount: float) -> void:
    if amount > 0:
        _balance += amount

func get_balance() -> float:   # C642 — getter method
    return _balance
# C642 — method calls on an object
var card: MykiCard = MykiCard.new()
card.top_up(10.0)
print(card.get_balance())

Earns the tick: a method defined in your own class and called through an object (card.top_up(...)). Calling built-in methods (text.to_upper()) shows use of the language but the code is for methods you wrote.

Contrast from connect-go-dots: get_children(), connect() and change_scene_to_file() are all method calls — on engine objects. None earn C642; the tick needs a method on a class you wrote.

In tile-matching-game — the positive case: flip_up(), flip_down() and mark_matched() are defined in card.gd (a class you wrote) and called through objects in game.gd:

# tile-matching-game — game.gd
first_card.mark_matched()    # C642 — your method, called on your object
second_card.mark_matched()

Ladder up: methods on your own class are the doorway to level 9–10 — classes (C651), objects (C652) and encapsulation (C654) live in OOP Concepts.

Check Your Understanding

  1. Why does _ready() not earn C641?
Answer

Godot calls it for you — the tick needs a function you wrote and call yourself.

  1. What three ingredients make full C641 evidence?
Answer

Typed parameter(s), a return value that gets used, and a call from elsewhere in the program.

  1. What turns a function into a C642 method?
Answer

It is defined in your own class and called through an object — card.top_up(10.0).

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9