Explaining "passing variables"

Posted By: ibra

Explaining "passing variables" - 01/16/13 08:46

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
Posted By: 3dgs_snake

Re: Explaining "passing variables" - 01/16/13 09:07

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.
Posted By: ibra

Re: Explaining "passing variables" - 01/16/13 09:54

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
Posted By: 3dgs_snake

Re: Explaining "passing variables" - 01/16/13 11:09

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).
© 2024 lite-C Forums