Gamestudio Links
Zorro Links
Newest Posts
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Data from CSV not parsed correctly
by EternallyCurious. 04/20/24 21:39
M1 Oversampling
by 11honza11. 04/20/24 20:57
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 177 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Explaining "passing variables" #415272
01/16/13 08:46
01/16/13 08:46
Joined: Jan 2013
Posts: 68
I
ibra Offline OP
Junior Member
ibra  Offline OP
Junior Member
I

Joined: Jan 2013
Posts: 68
Hi guys,

I'm trying to go through the tuts avaible in the manual and I have a question about "passing variables" section in workshop 2.

The thing is that I don't really understand how it works. Like in the example:

var compute_days(var age)
{
return age * 365.25;
}

function main()
{
var my_age = 33;
var number_of_days = compute_days(my_age);
printf("I am %.f days old!",number_of_days);
}

Exactly where does "var age" come from? And how can the computer compute "return age * 365,25"? The "age" doesn't have a value? or?

If someone could explain it, I would be thankful!


/Ibra

Re: Explaining "passing variables" [Re: ibra] #415273
01/16/13 09:07
01/16/13 09:07
Joined: Feb 2010
Posts: 320
TANA/Madagascar
3dgs_snake Offline
Senior Member
3dgs_snake  Offline
Senior Member

Joined: Feb 2010
Posts: 320
TANA/Madagascar
Code:
var compute_days(var age)
{
   return age * 365.25;
}


You are creating a function that accepts a var argument. This argument will be known as age inside the function(you declared it as so). The functin will return another var resulting from the calculation declared inside your function.

Code:
function main()
{
   var my_age = 33;
   var number_of_days = compute_days(my_age);
   printf("I am %.f days old!",number_of_days);
}


You declare a var named my_age.
You call the function declared above and pass in the my_age variable (Inside the function it will be known as age and will have the value you passed in when you called the function) and put the value inside the number_of_days variable.
Finally, you print it.

Re: Explaining "passing variables" [Re: 3dgs_snake] #415275
01/16/13 09:54
01/16/13 09:54
Joined: Jan 2013
Posts: 68
I
ibra Offline OP
Junior Member
ibra  Offline OP
Junior Member
I

Joined: Jan 2013
Posts: 68
Originally Posted By: 3dgs_snake
Code:
var compute_days(var age)
{
   return age * 365.25;
}


You are creating a function that accepts a var argument. This argument will be known as age inside the function(you declared it as so). The functin will return another var resulting from the calculation declared inside your function.

Code:
function main()
{
   var my_age = 33;
   var number_of_days = compute_days(my_age);
   printf("I am %.f days old!",number_of_days);
}


You declare a var named my_age.
You call the function declared above and pass in the my_age variable (Inside the function it will be known as age and will have the value you passed in when you called the function) and put the value inside the number_of_days variable.
Finally, you print it.


Hi 3dgs,

Thanks for your fast reply. I think i got it now. The thing that got me confused is that I think that instead of var compute_days(var age) it should say var compute_days(var my_age).

I don't really get how the computer know its the same thing? or is it?

AND, passing variables, is this a usefull tool? or is it more to make the coding "look good"?

Thank you!

Ibra

Re: Explaining "passing variables" [Re: ibra] #415281
01/16/13 11:09
01/16/13 11:09
Joined: Feb 2010
Posts: 320
TANA/Madagascar
3dgs_snake Offline
Senior Member
3dgs_snake  Offline
Senior Member

Joined: Feb 2010
Posts: 320
TANA/Madagascar
The name you use when declaring a function parameter is used to allow you manipulate the variable inside the function. It is like a placeholder (eg age). When calling your function, you pass in the real variable you want to manipulate (my_age). You can name them the same but internally, that won't change anything. Look here for more explanation if you want.

Functions are useful to allow you to have a better structured code (to group some common operations in one place instead of repeating it over and over).


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1