Login
Username:

Password:

Remember me



Lost Password?

Register now!
Main Menu
Who is Online
33 user(s) are online (24 user(s) are browsing Forum)

Members: 0
Guests: 33

more...


Browsing this Thread:   1 Anonymous Users






Re: Sending values to procedures by reference in PortablE
#2
Home away from home
Home away from home


See User information
There are several ways you could do this. The way which requires least changes to your code is:
Quote:
<pre>
OPT POINTER

PROC main()
DEF a:INT

a:= 3
changeme (ADDRESSOF a)
Print (' \d \n',a)
ENDPROC

PROC changeme(b:PTR TO INT)
b[0]:=7
ENDPROC</pre>


However I recommend using ARRAY OF instead of PTR TO:
Quote:
<pre>
OPT POINTER

PROC main()
DEF a:INT

a:= 3
changeme (ADDRESSOF a)
Print (' \d \n',a)
ENDPROC

PROC changeme(b:ARRAY OF INT)
b[0]:=7
ENDPROC</pre>


Here is one way of doing it without OPT POINTER, although it's probably too fiddly to really use:
Quote:
<pre>
PROC main()
DEF a[1]:ARRAY OF INT

a[0]:= 3
changeme (a)
Print (' \d \n',a[0])
ENDPROC

PROC changeme(b:ARRAY OF INT)
b[0]:=7
ENDPROC</pre>


However, I would recommend using objects:
Quote:
<pre>
OBJECT container
a:INT
ENDOBJECT

PROC main()
DEF c:container

c.a:= 3
changeme (c)
Print (' \d \n',c.a)
ENDPROC

PROC changeme(c:PTR TO container)
c.a:=7
ENDPROC</pre>


Here is another way of doing it with objects:
Quote:
<pre>
OBJECT container
a:INT
ENDOBJECT

PROC main()
DEF c:PTR TO container
NEW c

c.a:= 3
changeme (c)
Print (' \d \n',c.a)
FINALLY
END c
ENDPROC

PROC changeme(c:PTR TO container)
c.a:=7
ENDPROC</pre>


If your object becomes more complex, you might want to consider adding methods to your object. But for your simple example the extra complexity looks silly:
Quote:
<pre>
CLASS container
a:INT
ENDCLASS
PROC new(a=0:INT) OF container
self.a := a
ENDPROC
PROC set(a:INT) OF container
self.a := a
ENDPROC
PROC get() OF container IS self.a

PROC main()
DEF c:PTR TO container
NEW c.new()

c.set(3)
changeme (c)
Print (' \d \n',c.get())
FINALLY
END c
ENDPROC

PROC changeme(c:PTR TO container)
c.set(7)
ENDPROC</pre>


P.S. I did not test any of this code, so it is possible I made an error which prevents it from compiling. If that is so, then please let me know.

Posted on: 2013/10/2 18:06
ChrisH
--
Author of the PortablE programming language.
 Top  Twitter  Facebook  Google Plus  Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Sending values to procedures by reference in PortablE
#1
Just can't stay away
Just can't stay away


See User information
I want improving my coding skills so I want ask how in PortablE send value into procedure by reference. Sending value in this way not create new temporary local variable, and changes in procedure are visible level higher even if value is local - not global. It can be useful for some speed optimisations because creating temporary values take some time.

For example I try create code but this code is not compiling because I not know proper PortablE language semantic.

Quote:
<pre>
PROC main()
DEF a:INT

a:= 3
changeme (a)
Print (' \d \n',a)
ENDPROC

PROC changeme(b:PTR TO a)
b:=7
ENDPROC</pre>


I expect result of this program = 7 because b is pointered to a

How write this in proper way?

Posted on: 2013/10/2 17:39
 Top  Twitter  Facebook  Google Plus  Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 







You can view topic.
You cannot start a new topic.
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[Advanced Search]


Search
Top Posters
1 paolone
paolone
4462
2 nikolaos
nikolaos
4206
3 magorium
magorium
4095
4 phoenixkonsole
phoenixkonsole
3942
5 deadwood
deadwood
2917
6 ncafferkey
ncafferkey
2810
7 mazze
mazze
2222
8 Kalamatee
Kalamatee
2212
9 clusteruk
clusteruk
2114
Powered by XOOPS © 2001-2025 The XOOPS Project