Godot - Function Parameters & Arguments question

Hi folks, just curious as I’m trying to get my head around the benefits of having parameters in the functions, so I tried to do this thinking I’d multiply 10, 11 and 12 by 2. But instead I got the Output as 20, 22, and 24. Code as follows:

extends RigidBody2D


# Called when the node enters the scene tree for the first time.
func _ready():
	printDoubled(10, 11, 12)
	
	
func printDoubled(input_number1, input_number2, input_number3):
	print(input_number1*2)
	print(input_number2*2)
	print(input_number3*2)

Hi,

the benefit of having parameters in a function is firstly it makes things more human readable at the point of someone using the function, so they know what the function expects to be given, it does make it a bit easier when trying to understand someone elses code, as you know what the function is expecting and in what order.

for an example if we want to create a function that outputs the area of carpet we need for a floor, given the width and length, we can have

func Show_floor_area(_width, _length):
  var area = _width * _length
  print (area)
  return area

so when it comes to using the function
when you start typing the function you will probably see the hint appear, like this, showing that its looking for _width firstly, then _length
image

what you have in that code snippet is fine.

what your function is expecting is three things in a certain order.

when you called printDoubled(10, 11, 12), you did that, you have supplied the function with the 3 things it is expecting.

so replacing the values the function is expecting with what you have supplied, that would give you.

func printDoubled(input_number1, input_number2, input_number3):
print(10 * 2) # prints 20
print(11 * 2) # prints 22
print(12 * 2) # prints 24

function parameters can be very powerful in helping us use and debug code / functions.
depending on what you want the function to do, the order it receives the arguments can be very important.
later on in the course you will cover how we can further restrict the type of information a function is expecting (be that numbers or a text string), this way if we accidentally dont give the function the correct information, it will flag a warning to help us.

think ive rambled a bit there sorry :frowning:

No no, that’s an excellent explanation thank you! It’s just I realised my name is mud backwards because I didn’t think that it was doing what I expected but it did. Had a really dense moment mathematically… :rofl: thank you!

1 Like

no worries at all, thats what everyones here for is to learn, ive been there and when your in that ‘cant see the woods for the trees’ moment, talking about it helps :slight_smile:

1 Like

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

Privacy & Terms