Menu
CoddyTech

Default Arguments

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

C++ and GDScript both support trailing default arguments and ordinary positional calls. Neither provides arbitrary named-argument calls through these declarations. Place required parameters first and preserve order. A GDScript default does not create overloaded methods; C++ overload resolution and default-argument visibility have their own rules. Construct mutable per-instance containers deliberately rather than assuming a default expression implies a desired ownership model. The example repeats text and does not implement the task delimiter framing.

Complete C++ comparison. Save the listed files together and compile the .cpp files with C++20; main is the entry point.

C++ file main.cpp:

#include <iostream>
#include <string>
std::string repeat(const std::string &text,int count=2) { std::string out; for(int i=0;i<count;++i) out+=text; return out; }
int main() { std::cout << repeat("ha") << '\n'; }

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