Basics questions about function creation
-
Hello, i'm a coding beginner and function creation is a little confusing for me. I don't understand what i can do and what i can't do with it exactly. For exemple, i just finished the "Structures" tutorial and I saw that we can code a loop inside an own made function. It was very surprising for me. So here are the questions :
-
Can a custom function contain a "if...then....else....endif." loop, a while loop, a for loop, a structure, an array, another custom function ? Can you give some easy to understand little coding examples ? I think i'm for now not really aware about all the things we can do with that tool.
-
The return part is slightly confusing for me. What kind of value can a function exactly return ? First, can a custom function return more than one value ? I tried to adapt the screen tutorial but this time with a drawn text instead of a circle. I tried to do myself some own custom functions to code the drawn text movement but because i don't figure how a function could return two values, i need to do one for the movement of the x axis and one for the movement one the y axis. I mean, can i code the movement on both axis only in one function ? If yes, can you show me how ? Can a function only return a number ? Can it return a string for exemple or a math operation ?
I'm aware that these questions are very simples for you but it's really the first time i code.
By the way, this is the ID on my first code (still on pending status right now) : VMZ63MNDH4
-
-
I’m no expert, but as far as I know, you can kindof treat a function like any other part of your program. So yes, you can put in loops, variables, calls to other functions, all that stuff. I don’t actually know if you can define a function inside another function - it wouldn’t make any sense to do something like that - but I wouldn’t be at all surprised if it was possible.
As far as I know, fuze can return only one thing per function. that thing doesn’t have to be just one value though - you could return a vector, which is 4 numbers bundled together like {100,100,50,1} - or you could return a string or a struct or a whole array of stuff. So just being able to return one thing isn’t so bad as you might think.
For your function that you’re doing with x and y values, have it return a vector.
-
So for the first question, you can put anything you want in a function. When the function is called, program execution jumps to the function, and there are really no restrictions on what you want to happen in the function. You can have an infinite loop in there and never return to the main code if you should want to.
Note that any variables that you declare inside the function will be local to that function, so they will not be accessible once you return from the function. Therefore, you would often want to return some value using the return statement.
You can return whatever you want: structs, string, arrays, numbers, vectors. If you want to return two values you can for instance use a vector like this:
function myFunc() local_x = 100 local_y = 200 return {local_x, local_y}
This function returns a vector. Vectors are nice, especially when dealing with coordinates, since the first two values can be accessed with vectorName.x and vectorName.y.
So if I would call the function above and use the result, I would do this:
myVector = myFunc() print("x: ", myVector.x) print("y: ", myVector.y) update()
This will print the following on the screen:
x: 100 y: 200
-
@Fifine There are no stupid questions, especially when you are a beginner. Ask away, I know that lots of people here love to help! I know I do...
-
Just to add one clarification to what everyone else has said here - you cannot define another function inside a function, you will get an error when you try to run the code. But you can certainly call another function from within a function and this is perfectly normal to do. This might be a bit confusing, but you can also call a function in the return statement like this:
function myFuncB() // Do something (or not, entirely optional) return myFuncA()
What that will do is execute all the statements in function B and then return the result of calling function A (after having run all of it's statements too)
-
Thanks everyone for the answers
-
@Martin Ok thats interesting that you cant declare functions in fuctions but thanks martin for make things clear