Page 1 of 1

Easier way to pass parameters by reference, as in C ?

Posted: Thu Feb 22, 2024 9:44 am
by PBJim
I frequently need to pass by reference and return values and have always done this with a structure such as Procedure x(*param.Structure). Is there an easier method, such as the way it can be done more straightforwardly in C that doesn't require anything more than the pointer? I'm not certain if we're permitted to include code of other languages on this forum, but by way of example, the below shows only that which is relevant :

Code: Select all

void myfunc(int *x, int *y)
{
    (*x)++;
    (*y)++;
Then in the call :

Code: Select all

myfunc(&x, &y);

Re: Easier way to pass parameters by reference, as in C ?

Posted: Thu Feb 22, 2024 1:49 pm
by STARGÅTE
PBJim wrote: Thu Feb 22, 2024 9:44 am Is there an easier method, such as the way it can be done more straightforwardly in C that doesn't require anything more than the pointer
Isn't it similar easy in Pure Basic:

Code: Select all

Procedure myfunc(*x.Integer, *y.Integer)
	*x\i + 1
	*y\i + 1
EndProcedure

Define a.i = 2, b.i = 5
myfunc(@a, @b)
Debug a
Debug b
There is no easier method as using a pointer with the specific elementary structure.

Re: Easier way to pass parameters by reference, as in C ?

Posted: Thu Feb 22, 2024 4:01 pm
by PBJim
STARGÅTE wrote: Thu Feb 22, 2024 1:49 pm Isn't it similar easy in Pure Basic:
Thanks, I appreciate the example, that's in fact considerably easier than the method I've been using. I've always used the method at the below link. It requires the variable being passed into the procedure also to have been defined with a structure, in addition to the structure within the procedure. It has been fairly labourious work, because I've converted existing code to return values by ref.

https://www.purebasic.fr/english/viewto ... 09#p544509

Re: Easier way to pass parameters by reference, as in C ?

Posted: Thu Feb 22, 2024 7:41 pm
by Tenaja
(I think) that method with a string is used because a string is already a pointer, and when you call a proc with a string argument, by default it copies the string prior to sending. That is a workaround specific to strings to send the string address.

Re: Easier way to pass parameters by reference, as in C ?

Posted: Thu Feb 22, 2024 8:14 pm
by PBJim
Tenaja wrote: Thu Feb 22, 2024 7:41 pm (I think) that method with a string is used because a string is already a pointer, and when you call a proc with a string argument, by default it copies the string prior to sending. That is a workaround specific to strings to send the string address.
Thank you Tenaja, I see what you mean. For strings, it's still not quite so straightforward then. Ah well, worth a try. Thanks both for the replies.

Re: Easier way to pass parameters by reference, as in C ?

Posted: Thu Feb 22, 2024 11:00 pm
by mk-soft
Strings are a special case here, because string variables are a pointer to the string.
To work with String ByRef, you have to pass the pointer to the string pointer. Unfortunately, this is not possible with standard string variables, but only with structure variables.

Code: Select all

;-TOP by mk-soft

; String ByRef

Procedure MyFunction(var1, var2, *sResult.string)
  *sResult\s + Str(var1 + var2)
  ProcedureReturn Len(*sResult\s)
EndProcedure

Define sVar.String ; <- pre defined pb structure for string
Define len

sVar\s = "Result = "
len = MyFunction(100,100, @sVar)
Debug "String Len = " + len
Debug sVar\s

Re: Easier way to pass parameters by reference, as in C ?

Posted: Fri Feb 23, 2024 12:54 am
by AZJIO