Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Help with FUNCTION parameters

Author
Message
lovecraft58
9
Years of Service
User Offline
Joined: 30th Nov 2014
Location: United States
Posted: 3rd Dec 2014 19:08
I'm confused. I don't understand the passing of parameters to and from a FUNCTION. It seems sometimes you need empty parentheses, sometimes with arguments, and sometimes with neither. I don't get it. Also, can I NOT use the same variable name from main body to FUNCTION? Why? Thanks to anyone who can simplify this for me.

Nick Z.

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 4th Dec 2014 01:56
Hi, I wrote a bit of code to try and demonstrate using functions. I tried to comment things out to show you what it is doing. It is quite simple and does not have gravity or collision or anything. Hopefully this will help.



So many games to code.....so little time.
lovecraft58
9
Years of Service
User Offline
Joined: 30th Nov 2014
Location: United States
Posted: 4th Dec 2014 08:08
Thanks, LBFN, but now I'm even more confused. Should I just make everything a GLOBAL? The examples in "Hands on DarkBasic Pro" don't always do that. And why the (1) in MoveCube? And the "variable = FUNCTION" syntax in MoveCube and AddScore but just the FUNCTION in MoveCam? Is that the difference between passing and not passing a variable? How does the program know which variable is being returned?

hope I'm not too annoying.

The code is a bit advanced for me.

Nick Z.

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 4th Dec 2014 15:12
Sorry for the confusion, that is the last thing that hoped would happen.

Quote: " Should I just make everything a GLOBAL?"

You could, if that is your preference, but you certainly do not have to do that. I normally only make global those variables that I intend to use in functions.

Quote: "And why the (1) in MoveCube?"

I am passing to MoveCube the number of the object I want to move. This gives you the flexibility to move different objects using the same function. So, by using 1, which is the number of the cube that I created to move, it acts upon object 1 in the function. Perhaps it would be easier to understand if you consider that whatever I have in the brackets when the function call is made is placed into the variable in the actual function. So, in this case, the variable, 'ObjToMove' is assigned the value of 1. If I created a sphere with the object number 14 I would make the call as MoveCube(14) - this would pass 14 to the ObjToMove variable.

Quote: "
And the "variable = FUNCTION" syntax in MoveCube and AddScore but just the FUNCTION in MoveCam? Is that the difference between passing and not passing a variable?"


The beauty of functions is that they can return a value. This can be very powerful and to me, is one of the biggest reasons why you would use them instead of subroutines. However, you still can use a function without returning anything. Understand that the variable being called does not have to be the same as what it is in the function. Take this code and replace the MoveCube() function in the original code:




So count is not in this function at all, yet I am changing the value of count based upon how I move the cube. The only reason at this point that I need count to be global is that it is in the Debug() function. I normally would use a variable that is more descriptive, but left in count just for this example. Anyway, you can see how that I can know which direction I am moving. You could even take it a step further and rotate the object based upon the value in count or play a specific animation or shoot a gun or .... (you fill in the blank).

Quote: "How does the program know which variable is being returned? "

The syntax of the function call will determine that. Bear in mind that if you declare MoveCam(1) and don't have a variable in the brackets of the function, it will error. It would also error if you try to return a value and simply called MoveCube().

I really want to help. If this is still confusing, let me know and I will try another approach.

So many games to code.....so little time.
lovecraft58
9
Years of Service
User Offline
Joined: 30th Nov 2014
Location: United States
Posted: 4th Dec 2014 22:12
LBFN< thanks. You've given me a lot to chew on. I'll work with what you've shared and I'll post any more questions I have as they come up.

Nick Z.

Login to post a reply

Server time is: 2024-11-27 05:42:44
Your offset time is: 2024-11-27 05:42:44