PDA

View Full Version : passing variables to functions



sandyrepope
29-08-2007, 01:16
I hate to admit it but I'm having trouble understanding about how to pass variables to a function. For some reason I'm just not getting it.

Is there somewhere I can read about this so that hopefully I'll understand it?

Thanks
Sandy

kryton9
29-08-2007, 05:33
Sandy, it is hard to start with them. You can pass parameters (variables or values) to subroutines and functions.
Functions return a value back, subroutines don't.

So here is the same program, one using a subroutine and the other a function, so you can see the difference:

' this is the subroutine version
uses "console"

dim a,b,c as long
global answer as long

a = 5 : b = 3 : c = 2

goAdd(a,b,c)
Console_WriteLine ("the answer is", answer)

sub goAdd(firstParameter as long, secondParameter as long, thirdParameter as long)
'since a sub does not return a value we are assigning it to the global variable answer
answer = firstParameter + secondParameter + thirdParameter
end sub

console_waitkey(10)
console_cls


' this would work also by passing the values directly as parameters
' uses "console"

' global answer as long
'
' goAdd(5,3,2)
' Console_WriteLine ("the answer is", answer)
'
' sub goAdd(firstParameter as long, secondParameter as long, thirdParameter as long)
' 'since a sub does not return a value we are assigning it to the global variable answer
' answer = firstParameter + secondParameter + thirdParameter
' end sub

' console_waitkey(10)
' console_cls

The function version:

' this is the function version
uses "console"

dim a,b,c as long

a = 5 : b = 3 : c = 2

Console_WriteLine ("the answer is", goAdd(a,b,c))

function goAdd(firstParameter as long, secondParameter as long, thirdParameter as long)
'this function is just the keyword to return a value from the function
function = firstParameter + secondParameter + thirdParameter
end function

console_waitkey(10)
console_cls


'this would work also by passing the values directly as parameters
' uses "console"

' Console_WriteLine ("the answer is", goAdd(5,3,2))
'
' function goAdd(firstParameter as long, secondParameter as long, thirdParameter as long)
' 'this function is just the keyword to return a value from the function
' function = firstParameter + secondParameter + thirdParameter
' end function

' console_waitkey(10)
' console_cls

Petr Schreiber
29-08-2007, 09:47
Hi,

Kent already did a good example, I will expand more on BYCOPY and BYREF.

In thinBASIC you can have function and procedures ( SUBs ) with up to 32 parameters.
Passing values using parameters helps to minimize use of global variables.

You can pass parameters using two ways

BYCOPY - default
BYREF


First option is default, and it means whatever variable you pass, only its copy will be used, but it cannot be changed from inside of function. This is safe, but not fastest, as there must be created copy of variable internally.

Sample demo is here:


Main()

sub Main()
local NameOfDog as string = "Laika" ' :)

DisplayNameOfDog(NameOfDog)

MSGBOX 0, "Contens of ""NameOfDog"": "+NameOfDog+" ( did not changed really )",, "Main()"
end sub

sub DisplayNameOfDog( Name as string )

MSGBOX 0, "Well, you passed following name:"+$CRLF+$CRLF+Name, , "From SUB DisplayNameOfDog"

' -- Parameter name was passed BYVAL ( default ), so to assign new value to parameter
' -- has effect only inside function, it will not "damage" original data
Name = "Goofy"

msgbox 0, "I have destroyed your parameter passed in body of procedure, new name is: "+Name, , "From SUB DisplayNameOfDog"

end sub


But in some cases you want to change contens of passed variable. Imagine you need to return 3 values.
Classic function can return only one value. Here comes passing parameters BYREF, which means "by reference".
This way no need to copy the contens of passed data, only pointer to data is passed ( just internally, you do not have to care about pointers at all ).
You can pass only variables using BYREF, no numbers or text between quotes.

So here is sample:


Main()

sub Main()
local child1, child2, child3 as string

GetDogChildren("Laika", child1, child2, child3)

MSGBOX 0, "Children of Laika are "+child1+", "+child2+", "+child3 ,, "Main()"
end sub

sub GetDogChildren( Name as string, BYREF first as string, BYREF second as string, BYREF third as string )

if Name = "Laika" then
first = "Rex" ' -- This will actually change contens of variable passed to "first" even outside of the function
second = "Bella"
third = "Foodog"
end if

end sub

As you can see, first parameter is passed BYCOPY, so I could pass normal text, no need to have it in variable.
But BYREF parameters need to be passed as variables.

Hope this put light on the problem at least a bit.


Petr

Michael Hartlef
29-08-2007, 11:26
BYCOPY is a very uncommon term, most languages use the BYVAL term. Just in case Sandy never heard about it.

Petr Schreiber
29-08-2007, 12:39
Hi Mike,

BYCOPY is not BYVAL. I thought thinBASIC uses BYVAL, but tB help file says BYCOPY.

About BYCOPY from PowerBASIC manual:


A parameter passed by copy is a special case; somewhat of a hybrid of the other two methods ( meant BYVAL, BYREF ). When a procedure expects a parameter to be passed by reference, it expects to see a pointer to the data. In some cases, such as when the parameter is a calculated expression, it is not precisely possible to pass a pointer, since an expression result is a temporary value that does not exist in a permanent memory location.

...

In both cases, a copy of the data is stored in a temporary memory location, and the parameter consists of a 4-byte address of this temporary location.


Hope it is more clear. But I think it can be used as with BYVAL.


Bye,
Petr

sandyrepope
29-08-2007, 16:08
Thanks to all who posted. I'll study all that information today. There is one thing I'm not clear on. I still don't understand how this is better than using global variables but assume that I'll get it when I have a chance to study the post.

If I have more questions I'll post them here.

Thanks
Sandy

ErosOlmi
29-08-2007, 23:52
Sandy,

when you create a function or a sub, it is a good programming practice to create them as much general as possible (ie, not dependant from any global variable declaration). This because functions and sub should be re-used in many places or even placed in included files to be used from many different scripts.

If a function or a sub is depending on an already declared global variable, it is not general but specific to that script where the global variable is declared.
When you create functions or subs think sothing like: I'm using global variables? If yes, in most of the cases you are missing an opportunity.

Trying to avoid global variable is something every programmer have to think about always. Of course I'm talking about medium, big projects and not example scripts.
One of the main reasons to avoid globals is because it is too much easy to change them along the code. When program start becoming quite complex (let say more than some hundred lines) if something goes wrong it is very hard to find where a global variabḷe can be changed. While if you have independent functions or sub (that do not use global variales but just local one or parameters) it is much more easy to follow program flow.

Ciao
Eros

sandyrepope
30-08-2007, 00:47
I think I've got the idea now. Is what you mean called reusable code? I've been reading a little bit about that lately. If that is what you mean then I finally understand.

By writing functions that use variable passing I'll have functions that I can keep and then when a project needs one or more of them I can just copy/paste them in without having to modify them to match perticular variables that the script is using.

Thank you
Sandy

RobertoBianchi
30-08-2007, 09:35
Hi Sandy,

reusable code it is a enough complex concept.
As you say copy and paste can speed up writing your program but this is only a first step. A second step could be file inclusion but still you are working with 'reusable source code'. In case of interpreter or some compilers (PB for example) the discussion ends here otherwise others compilers give us the possibility to reuse binary code stored into object files or static libraries. This opportunity allows programmers to share well know, not changeable and tested code into different project. Of course with the introduction of DLLs' (MS from always takes care of programmers' productivity!) the reusable code boost because the OS itself give us an enormous amount of already done code.

Ciao,
Roberto

sandyrepope
30-08-2007, 15:47
I understand now and in the future I'll be using variable passing in my projects.

I'd like to thank everyone who helped me to understand it. Everyone here is so patient with me as I work to understand more about thinBasic.

Thanks
Sandy

ErosOlmi
30-08-2007, 18:35
Sandy, it is a pleasure to help everyone here.
I think from discussions like this one I can also keep some concept and put into thinBasic help.
So thanks to you for posting code and questions.

Eros