Blame
|
1 | <!-- Generated from applied-computing-au vic/unit3-4/sat/C06-2026/C06-Reference-Godot — do not hand-edit; re-run the port. --> |
||||||
| 2 | # Functions and Methods — GDScript |
|||||||
| 3 | ||||||||
| 4 | ||||||||
| 5 | **Skill codes on this page** |
|||||||
| 6 | ||||||||
| 7 | | Code | Level | Skill | |
|||||||
| 8 | |---|---|---| |
|||||||
| 9 | | C641 | 7–8 | functions | |
|||||||
| 10 | | C642 | 7–8 | methods | |
|||||||
| 11 | ||||||||
| 12 | **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. |
|||||||
| 13 | ||||||||
| 14 | ## C641 — Functions |
|||||||
| 15 | ||||||||
| 16 | **Definitions.** |
|||||||
| 17 | ||||||||
| 18 | - *Function:* a sequence of related code that has been given a name that can be called from other points in the source code. |
|||||||
| 19 | ||||||||
| 20 | - *Arguments / parameters:* specific inputs passed into a function that act as local, temporary variables. |
|||||||
| 21 | ||||||||
| 22 | - *Return value:* a value passed back to the caller, often assigned to a variable or tested in a condition. |
|||||||
| 23 | ||||||||
| 24 | ```gdscript |
|||||||
| 25 | # C641 — function: named, takes parameters, returns a value |
|||||||
| 26 | func calculate_fare(zone: int, is_concession: bool) -> float: |
|||||||
| 27 | var fare: float = FARE_TABLE[zone] |
|||||||
| 28 | if is_concession: |
|||||||
| 29 | fare *= 0.5 |
|||||||
| 30 | return fare |
|||||||
| 31 | ``` |
|||||||
| 32 | ||||||||
| 33 | ```gdscript |
|||||||
| 34 | # C641 — function call: result assigned and used |
|||||||
| 35 | var fare: float = calculate_fare(2, true) |
|||||||
| 36 | ``` |
|||||||
| 37 | ||||||||
| 38 | **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. |
|||||||
| 39 | ||||||||
| 40 | **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. |
|||||||
| 41 | ||||||||
| 42 | **In tile-matching-game** (`deck.gd` → `game.gd`) — the full tick in real code: typed parameter, typed return, result used: |
|||||||
| 43 | ||||||||
| 44 | ```gdscript |
|||||||
| 45 | # tile-matching-game — deck.gd |
|||||||
| 46 | static func load_csv(path: String) -> Array: # C641 — typed parameter and return |
|||||||
| 47 | var cards: Array = [] |
|||||||
| 48 | # … one Dictionary per CSV row … |
|||||||
| 49 | return cards |
|||||||
| 50 | ``` |
|||||||
| 51 | ||||||||
| 52 | ```gdscript |
|||||||
| 53 | # tile-matching-game — game.gd |
|||||||
| 54 | var all_cards := Deck.load_csv(deck_path) # C641 — call: the result drives the board |
|||||||
| 55 | ``` |
|||||||
| 56 | ||||||||
| 57 | **Say the types out loud in validation:** parameters and return are typed (`zone: int`, `-> float`) — that evidence also feeds C628. |
|||||||
| 58 | ||||||||
| 59 | ## C642 — Methods |
|||||||
| 60 | ||||||||
| 61 | **Definition.** An action an object can carry out (e.g. `window.refresh`, `golfClub.swing`). |
|||||||
| 62 | ||||||||
| 63 | ```gdscript |
|||||||
| 64 | class_name MykiCard |
|||||||
| 65 | extends RefCounted |
|||||||
| 66 | ||||||||
| 67 | var _balance: float = 20.00 |
|||||||
| 68 | ||||||||
| 69 | # C642 — method: an action the MykiCard object can carry out |
|||||||
| 70 | func top_up(amount: float) -> void: |
|||||||
| 71 | if amount > 0: |
|||||||
| 72 | _balance += amount |
|||||||
| 73 | ||||||||
| 74 | func get_balance() -> float: # C642 — getter method |
|||||||
| 75 | return _balance |
|||||||
| 76 | ``` |
|||||||
| 77 | ||||||||
| 78 | ```gdscript |
|||||||
| 79 | # C642 — method calls on an object |
|||||||
| 80 | var card: MykiCard = MykiCard.new() |
|||||||
| 81 | card.top_up(10.0) |
|||||||
| 82 | print(card.get_balance()) |
|||||||
| 83 | ``` |
|||||||
| 84 | ||||||||
| 85 | **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. |
|||||||
| 86 | ||||||||
| 87 | **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. |
|||||||
| 88 | ||||||||
| 89 | **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`: |
|||||||
| 90 | ||||||||
| 91 | ```gdscript |
|||||||
| 92 | # tile-matching-game — game.gd |
|||||||
| 93 | first_card.mark_matched() # C642 — your method, called on your object |
|||||||
| 94 | second_card.mark_matched() |
|||||||
| 95 | ``` |
|||||||
| 96 | ||||||||
| 97 | **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](/sd/C06/OOP%20Concepts). |
|||||||
| 98 | ||||||||
| 99 | ## Check Your Understanding |
|||||||
| 100 | ||||||||
| 101 | 1. Why does `_ready()` not earn C641? |
|||||||
| 102 | ||||||||
| 103 | >| ### Answer |
|||||||
| 104 | >| Godot calls it for you — the tick needs a function you wrote *and* call yourself. |
|||||||
| 105 | ||||||||
| 106 | 2. What three ingredients make full C641 evidence? |
|||||||
| 107 | ||||||||
| 108 | >| ### Answer |
|||||||
| 109 | >| Typed parameter(s), a return value that gets used, and a call from elsewhere in the program. |
|||||||
| 110 | ||||||||
| 111 | 3. What turns a function into a C642 method? |
|||||||
| 112 | ||||||||
| 113 | >| ### Answer |
|||||||
| 114 | >| It is defined in your own class and called through an object — `card.top_up(10.0)`. |
|||||||
