This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

I want to print "Hello" if I don't set an argument. And how is it possible functions not to have necessarily an argument/all possible agruments so it would be ignored then?

extends Node

func _on_Button_pressed():
say_something("bye")

func say_something(string)
print(string)
in Engine by (382 points)

1 Answer

+6 votes
Best answer

You can specify default values for arguments like:

func say_something(string = "Hello"):
    print(string)

With that...

say() # will print "Hello"
say("Bye") # will print "Bye"
by (22,704 points)
selected by

Thank you very much!

Is it a bug if this doesn't work: func say_something(string:String="Hello",a)
Seems that if you have set a default to a paramater before another parameter without a default, you have to set a default to the parameter after the first parameter which has a default.
Error message: Default parameter expected.
func say_something(a,string:String="Hello") works

No, that's not a bug. Arguments with default values must be at the end of the function's argument list. That's a fairly common restriction in a number of languages.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.