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

Just starting out? Need help? Post your questions and find answers here.
PBJim
User
User
Posts: 31
Joined: Fri Jan 19, 2024 11:56 pm

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

Post 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);
User avatar
STARGÅTE
Addict
Addict
Posts: 2090
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

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

Post 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.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
PBJim
User
User
Posts: 31
Joined: Fri Jan 19, 2024 11:56 pm

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

Post 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
User avatar
Tenaja
Addict
Addict
Posts: 1949
Joined: Tue Nov 09, 2010 10:15 pm

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

Post 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.
PBJim
User
User
Posts: 31
Joined: Fri Jan 19, 2024 11:56 pm

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

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

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

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply