Menu
CoddyTech

Default Arguments

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

PHP supports default parameters and named arguments. GDScript supports trailing defaults but ordinary calls remain positional; named PHP calls must be rewritten in declared order. Put required parameters before optional ones. Defaults do not mean every call must supply the same number of arguments. The example repeats text without adding separators, leaving the delimiter task for the learner.

Complete PHP 8.3 program files (run main.php with the CLI):

PHP file main.php:

<?php
declare(strict_types=1);
function repeatText(string $text,int $count=2): string { return str_repeat($text,$count); }
echo repeatText("ha"),"\n";

PHP output:

haha

Complete GDScript example files:

GDScript 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 PHP Developers

Practice on your own: Online GDScript compiler