Number Operations & More Printing

Question: how is it that a function can be declared after the function that calls it?!

In the example used in the lecture the challenge solutions looks something like this:

# Called when the node enters the scene tree for the first time.
func _ready():
func test():
	print("This is my first function created from scratch!")

…I would have thought this would generate an error due to the _ready() function being called first. No?

1 Like

I’m am not much of a Godot guy, but I want to make sure this question gets answered, here is what I found:

In Godot, you can declare functions in any order within a script because the script is parsed before execution. This allows you to define functions after the point where they are called. When the script is parsed, all the function definitions are collected, and they become available for use throughout the script.

In your example, when the _ready() function is called, it will search for the test() function and execute it if it exists. The fact that the test() function is declared after the _ready() function does not generate an error because the parser has already scanned the entire script and knows about the existence of both functions.

This behavior is possible due to a process called “forward declaration.” When the script is parsed, the parser scans the entire script to gather information about the functions, signals, and variables used. As a result, it knows about the existence of all the functions before the execution begins.

In some programming languages, forward declaration is necessary to use a function before it is defined. However, in Godot’s scripting language, GDScript, forward declaration is not required because the script is parsed before execution, allowing functions to be called regardless of their position within the script.

1 Like

Morning :wave:

nice catch Christopher.

I can see where confusion lays with this one, and to be honest, I couldnt get exactly to the bottom of it.

gdscript is an interpreted language of sorts, so much like python its not compiled or using JIT ahead of time.

from what i understand, as Christopher mentioned, the scripts are parsed then converted to bytecode, this process is done way before any of the internal callback methods are used like _ready() etc.
so by the time _ready() is called the functions are available.

sorry its pretty vague, but its not something ive gone too far into with GD

1 Like

Really fantastic, thorough reply. Thank you for taking the time to look into it and respond. I hope your answer helps others in the community as well!

1 Like

Much appreciated! Thanks for weighing in.

Forward declaration is very interesting.

I wonder to myself, however…as a question of convention…if it’s better to declare functions “up front”…creating more readable, logical code.

Or, perhaps this sort of flexibility is a core benefit of the dynamic, real-time scripting nature of GDScript and other interpreted languages. Maybe I’ll find a use for this sort of flexibility someday!

its my first real forray into dynamic non compiled languages.

before i was mainly VB and C++ (and not that great at them either tbh), so i was used to having say method signatures at top, then getting consumed and all my declarations were at the end of the code file in consumption order.

for better or worse, i quite like it so far and its quite forgiving :slight_smile:

BTW, my family and I (from Minnesota, USA) will be taking a trip to Scotland this summer. Won’t make it as far as the Highlands, but we’ll be in Edinburgh for a few days. Can’t wait to see your country!

1 Like

Edinburgh is nice, quite a bit to see and do there.
hope you enjoy it :slight_smile:

(fingers crossed the weather stays in your favor tho)

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms