Tutorial 4: Basic Functions in GDScript

GDScript with Basic Functions

Declaring a function in GDScript is not difficult; just like what is shown in the image above. You can declare a function with func someFunctionName(some_variable): , but I prefer declaring it with specific types in order to reduce bugs and errors, especially when the program gets too large. I declare it this way func someFunctionName(some_variable: TYPE) -> TYPE: and make sure you indent the codes within the function. You can download the source code here.

This tutorial solves for the square of a base. The function is called with an input variable and returns a float type that is assigned to a variable sqr, which is then converted to 2 decimal places and into a string before printing into the Output console.

The Square of the Base

As for your practice, create a function that returns true if the input integer is an even number with the use of a modulus or remainder operator %.

Related Posts:

  • Tutorial 7: Basic Match Statement in GDScript GDScript Match Statement The Godot match statement is the equivalent of switch statement in other programming languages. It's a cleaner way of handling conditional statements. If a match statement can handle my conditions… Read More
  • Tutorial 9: Basic While Loop in GDScript GDScript While Loop The GDScript above shows the basic uses of while loop to increment and decrement values. In this example the decrement operator -= is introduced. You can download the source code here. In this tutoria… Read More
  • Tutorial 8: Basic For Loop in GDScript GDScript of Basic For Loop In this example, it shows the basic uses of a for loop statement. One is to increment and the other decrements the value of 'n'. There are other usages but for now let's tackle the common ones. … Read More
  • Tutorial 6: Basic Conditional Statements in GDScript GDScript of Basic Conditional Statements The basic GDScript conditional statements shown above is pretty much straightforward. It simply uses the if, elif and else statements. You can download the source code here. In th… Read More
  • Tutorial 5: Basic Functions in GDScript Part 2 GDScript for Determining an Even or Odd Number This is the second part of the function tutorial. Please study the previous tutorials before studying this one; it builds from the concepts of the past tutorials 1 to 4. The… Read More

0 comments:

Post a Comment

© 2020 by Emman Lijesta, all rights reserved. Powered by Blogger.