Home Blog Gaming

GDScript Functions

Godot Tutorials > GDScript Functions
A function is a block of code that performs a task. For example, you may have a function to display the player's score. You may also have a function to determine if the player passed a level.

Why Use Functions?

Functions are helpful for many reasons.

  1. Reuseable Code: Functions allow you to write code once and reuse it again and again.

  2. Limited Variable Scope: A variable's scope is where the program can access that variable. Functions are a way to limit your vairable scope. When you declare a variable inside of a function, that variable only lives inside that function. This means no code outside of the function can change the variable's value.

  3. Focus: Functions allow you to write code one block at a time. You can focus on writing and testing one function without worrying about the rest of your application.

  4. Readability: Seperating your code into individual functions makes your code much more readible.

  5. Organization: Functions organize your code into reuseable code blocks.

GDScript Functions all belong to a class. Classes are a way to group together related functions and variables. Functions can also be refered to as methods and variables can also ber refered to as properties. By default, GDScript functions return null. If you want a function to return a value, you will have to create a return statement.

User Defined VS Pre-defined

Some functions are defined by the user (programmer), while others are included with the programmer language. print() is an example of a pre-defined GDScript function. The people who created GDScript have already provided this function to you. In the next lesson we will look at pre-defined GDScript Array Functions.

GDScript User Defined Functions

Now let's learn how to make our own GDScript Functions! Let's start with a simple function that does not return anything. We will make a function that takes a person's name as a parameter, then prints a good morning message. A parameter is a variable that is passed into a function, so it can be manipulated, modified, or incorporated into the function's code.

Mouse Hover over the blue text in the description to highlight the code.

# Function: No Return
func print_message (name):
  print ("Good Morning ", name)

➼ func - Keyword indicating a function is being declared
➼ print_message - The name I choose for my function
➼ name - The function's parameter
➼ print ("Good Morning ", name) - The code that executes when the function is called

Now let's call our function using "Frank" as our parameter.

# Function: No Return
func print_message (name):
	print ("Good Morning ", name)

func _ready():
  # Pass in Frank


** Debug Process Started **
    OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
    Good Morning Frank
    ** Debug Process Stopped **

Now let's make a function that returns something! We will make a function name add_nums that takes in 2 numbers as it's parameters and returns the sum of those parameters.

Mouse Hover over the yellow text in the description to highlight the code.

Learn HTML on Linux with Commander Candy
Learn ReactJS VR on Linux with Commander Candy
Twitch: daisychaincosplay Live Coding!!!
Sexy Linux T-shirt
murks - Linux Game Dev
Linux Gaming King Hatnix on Twitch
jookia - DOS Bots
Virtual Private Network with Commander Candy
# Function: Returns the sum of its parameters
func add_nums(num1,num2):
	var sum = num1 + num2
	return sum

# Call add_nums with parameters 2 and 3 and store the result in my_sum
var my_sum = add_nums(2,3)

func _ready():
  # print result
  print (my_sum)

➼ func - Keyword indicating we are declaring a function
➼ add_nums - Name of the function
➼ num1,num2 - Function parameters
➼ function code - The code that executes when our function is called
➼ sum - A variable named sum is equal to the sum of our parameters added together
➼ return sum - The function returns the value of sum
➼ my_sum - We call the function add_nums with parameters (2,3) and set my_sum equal to the return value
➼ print (my_sum) - We print the value of my_sum


** Debug Process Started **
    OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
    ** Debug Process Stopped **

Healing Example

Next we will rewrite the healing code from our if statements lesson into a healing function.

extends Node
# Declare Arrays
var paladinSkills = [20,5,15,18,5,10]
var knightSkills = [13,15,10,5,15,10]

# Declare heal function
func heal (skillArray):
  var healing = 0
  var baseScore = skillArray [3] - skillArray [4]
  if baseScore > 10:
    healing += (baseScore / 5)
  return healing

# call function passing in paladinSkills
var paladin_healing = heal(paladinSkills)

# call function passing in knightSkills
var knight_healing = heal(knightSkills)

func _ready():
  # Print Results in a pretty message
  print ("Paladin Heals: ", paladin_healing)
  print ("Knight Heals: ", knight_healing)


** Debug Process Started **
    OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2)
    Paladin Heals: 2
    Knight Heals: 0
** Debug Process Stopped **


Write GDScript functions to perform the following tasks.
  1. Given the length and width of a rectangle, calculate the rectangle's area.
  2. Convert a given temperature from celsius to fahrenheit.
  3. Given a player's score, display a cute message including the score.

Help Coding Commanders Reach Our Goal!

Linux Gaming Blog - Coding Commanders
Palm Beach Techie - Diverisity in Tech
Linux Gaming Blog YouTube Introduction
Linux Battle Card RPG - Gänseblümchen
SteamWorld Quest: Hand of Gilgamech
Hatnix - King of Linux Gaming