Menu
CoddyTech

Default Arguments

Lesson 11 of 48 in Coddy's GDScript for C Developers course.

C functions have no default or named arguments: callers supply each argument, or a wrapper function supplies a conventional default. GDScript supports trailing default parameters but calls use positional order. Place required parameters before optional ones. A default does not create overloaded methods, and it does not let callers skip a middle argument by naming a later one. For mutable owned state, construct each instance container deliberately. The C wrapper below demonstrates omission through a separate function, while GDScript provides it directly.

Complete C comparison. Save the listed files together and compile the .c files; main is the entry point.

C file main.c:

#include <stdio.h>
void repeat(const char *text, int count) { for (int i = 0; i < count; ++i) fputs(text, stdout); }
void repeat_default(const char *text) { repeat(text, 2); }
int main(void) { repeat_default("ha"); putchar('\n'); return 0; }

C output:

haha

GDScript example file main.gd:

extends Node

func _ready() -> void:
	print(repeat_text("ha"))

func repeat_text(text: String, count: int = 2) -> String:
	return text.repeat(count)

GDScript output:

haha
challenge icon

Challenge

Easy

Implement static surround(text:String,left:String="[",right:String="]")->String in TextFrame. Solve uses defaults for one input and the supplied delimiters for three inputs.

Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.

Your code goes in text_frame.gd, exercise.gd. main.gd holds the supplied input/output code and cannot be edited. Keep each class_name type in its named file.

Try it yourself

extends Node

func contract_ok(data: Array) -> bool:
	if TextFrame.surround("q") != "[q]" or TextFrame.surround("q", "{", "}") != "{q}":
		return false
	return true

func _ready() -> void:
	var data: Array = JSON.parse_string(OS.read_string_from_stdin())
	if not contract_ok(data):
		print("API_CONTRACT_FAILED")
		return
	print("result=" + Exercise.solve(data))
quiz iconTest yourself

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

All lessons in GDScript for C Developers

Practice on your own: Online GDScript compiler