Hi everyone,
I am trying to call a Procedure by storing its name in a Variable, and just cannot get it to work.
I have several Procedures defined.
PROC_A
PROC_B
PROC_C etc...
The selection (_A, _B, _C etc) is stored in a variable, lets say A$
What I would like to do is call the relevant Procedure with something like,
PROC(A$)
So that the correct one is called depending upon the selection made, and what is therefore stored in A$
Is this even possible?
Thanks
John
Calling a Procedure using a Variable
-
- Posts: 177
- Joined: Mon 02 Apr 2018, 21:51
Re: Calling a Procedure using a Variable
Hello,
I think this is what you want:
http://www.bbcbasic.co.uk/bbcwin/manual ... directcall
I mean the part labelled "Indirect procedure and function calls", just in case your browser doesn't automatically open the page at that position.
PM
I think this is what you want:
http://www.bbcbasic.co.uk/bbcwin/manual ... directcall
I mean the part labelled "Indirect procedure and function calls", just in case your browser doesn't automatically open the page at that position.
PM
-
- Posts: 25
- Joined: Sat 02 Jun 2018, 08:02
Re: Calling a Procedure using a Variable
Hi Patrick,
Thank you for the link. Yes, this is what I am trying to achieve.
I tried the first example:-
pptr% = ^PROC1
REM ....
PROC(pptr%)
However, this crashes BBC 4 Win!
If I remove the ^ it no longer crashes the IDE.
Any idea's
Thank you for the link. Yes, this is what I am trying to achieve.
I tried the first example:-
pptr% = ^PROC1
REM ....
PROC(pptr%)
However, this crashes BBC 4 Win!
If I remove the ^ it no longer crashes the IDE.
Any idea's
-
- Posts: 177
- Joined: Mon 02 Apr 2018, 21:51
Re: Calling a Procedure using a Variable
Would you be able to post the code that's crashing for you? I tried to replicate what you've described, but it works fine without crashing in both BB4W and BBCSDL.
This is the code that I made in trying to replicate what you described:
If I remove the ^ as you describe, I get Syntax error.
PM
This is the code that I made in trying to replicate what you described:
Code: Select all
PROCdummy: REM at least one conventional PROC or FN call must have been made before you attempt to read a procedure or function pointer
pptr%=^PROC1
PROC(pptr%)
PRINT "Reached end of program"
END
DEFPROC1
PRINT"PROC1 called"
ENDPROC
DEFPROCdummy
ENDPROC
PM
Last edited by p_m21987 on Fri 29 Jun 2018, 15:40, edited 1 time in total.
-
- Posts: 78
- Joined: Sat 23 Jun 2018, 15:51
Re: Calling a Procedure using a Variable
Try this:
If you didn't run a PROC (any PROC) before you ran your code you would get a No such FN/PROC. But that is in the manual as well but perhaps not explained too well. The program is only scanned for PROC addresses when it has to execute one so until you run PROCdummy it does not know that PROC1 exists.
Z
Code: Select all
PROCdummy
pptr% = ^PROC1
REM ....
PROC(pptr%)
END
DEF PROC1
PRINT "Hello"
ENDPROC
DEF PROCdummy:ENDPROC
Z
-
- Posts: 78
- Joined: Sat 23 Jun 2018, 15:51
-
- Posts: 25
- Joined: Sat 02 Jun 2018, 08:02
Re: Calling a Procedure using a Variable
Hi Patrick,
Just replied, but its not appeared in the forum??
Try again.
10 PROC_Dummy
20 REPEAT
30 REPEAT
40 CLS
50 PRINT "1 - Menu Item 1"
60 PRINT "2 - Menu Item 2"
70 INPUT "Enter selection "; Selection%
80 UNTIL Selection% >0 AND Selection% <3
90 pptr%=^Selection%
100 PROC(pptr%)
110 PRINT : PRINT "Run again Y/N? "
120 REPEAT
130 Key$ = GET$
140 UNTIL INSTR("YNyn", Key$)
150 UNTIL INSTR("Nn", Key$)
160 QUIT
170 REM Procedure 1
180 DEF PROC1
190 INPUT "First number ";FirstNum%
200 INPUT "Second number ";SecondNum%
210 PRINT
220 Answer% = FirstNum% + SecondNum%
230 PRINT FirstNum%; "plus "; SecondNum%; " = ";Answer%
240 ENDPROC
250 REM -----------------------------
260 REM Procedure 2
270 DEF PROC2
280 INPUT "First number ";FirstNum%
290 INPUT "Second number ";SecondNum%
300 PRINT
310 Answer% = FirstNum% - SecondNum%
320 PRINT FirstNum%; " minus "; SecondNum%; " = ";Answer%
330 ENDPROC
340 REM -----------------------------
350 DEF PROC_Dummy
360 ENDPROC
I've included line numbers in case you need to refer to lines in my code.
Thanks
Just replied, but its not appeared in the forum??
Try again.
10 PROC_Dummy
20 REPEAT
30 REPEAT
40 CLS
50 PRINT "1 - Menu Item 1"
60 PRINT "2 - Menu Item 2"
70 INPUT "Enter selection "; Selection%
80 UNTIL Selection% >0 AND Selection% <3
90 pptr%=^Selection%
100 PROC(pptr%)
110 PRINT : PRINT "Run again Y/N? "
120 REPEAT
130 Key$ = GET$
140 UNTIL INSTR("YNyn", Key$)
150 UNTIL INSTR("Nn", Key$)
160 QUIT
170 REM Procedure 1
180 DEF PROC1
190 INPUT "First number ";FirstNum%
200 INPUT "Second number ";SecondNum%
210 PRINT
220 Answer% = FirstNum% + SecondNum%
230 PRINT FirstNum%; "plus "; SecondNum%; " = ";Answer%
240 ENDPROC
250 REM -----------------------------
260 REM Procedure 2
270 DEF PROC2
280 INPUT "First number ";FirstNum%
290 INPUT "Second number ";SecondNum%
300 PRINT
310 Answer% = FirstNum% - SecondNum%
320 PRINT FirstNum%; " minus "; SecondNum%; " = ";Answer%
330 ENDPROC
340 REM -----------------------------
350 DEF PROC_Dummy
360 ENDPROC
I've included line numbers in case you need to refer to lines in my code.
Thanks
-
- Posts: 177
- Joined: Mon 02 Apr 2018, 21:51
Re: Calling a Procedure using a Variable
Hello hinckleyj,
It looks like the problem with your code is, you're taking the address of integer variable Selection%, and then trying to call that address as a PROCedure.
I was able to get your program to work by changing line 90:
PM
It looks like the problem with your code is, you're taking the address of integer variable Selection%, and then trying to call that address as a PROCedure.
I was able to get your program to work by changing line 90:
Code: Select all
10 PROC_Dummy
20 REPEAT
30 REPEAT
40 CLS
50 PRINT "1 - Menu Item 1"
60 PRINT "2 - Menu Item 2"
70 INPUT "Enter selection "; Selection%
80 UNTIL Selection% >0 AND Selection% <3
90 pptr%=EVAL("^PROC"+STR$(Selection%))
100 PROC(pptr%)
110 PRINT : PRINT "Run again Y/N? "
120 REPEAT
130 Key$ = GET$
140 UNTIL INSTR("YNyn", Key$)
150 UNTIL INSTR("Nn", Key$)
160 QUIT
170 REM Procedure 1
180 DEF PROC1
190 INPUT "First number ";FirstNum%
200 INPUT "Second number ";SecondNum%
210 PRINT
220 Answer% = FirstNum% + SecondNum%
230 PRINT FirstNum%; "plus "; SecondNum%; " = ";Answer%
240 ENDPROC
250 REM -----------------------------
260 REM Procedure 2
270 DEF PROC2
280 INPUT "First number ";FirstNum%
290 INPUT "Second number ";SecondNum%
300 PRINT
310 Answer% = FirstNum% - SecondNum%
320 PRINT FirstNum%; " minus "; SecondNum%; " = ";Answer%
330 ENDPROC
340 REM -----------------------------
350 DEF PROC_Dummy
360 ENDPROC
-
- Posts: 78
- Joined: Sat 23 Jun 2018, 15:51
Re: Calling a Procedure using a Variable
Sorry to cross in the mail again. EVAL gets what you want but also look at ON PROC methods it may be more suited to your need.
The manual has this which is eerily similar.
In your case the change would only be to replace lines 90 and 100 with
Z
The manual has this which is eerily similar.
Code: Select all
CLS
PRINT "Select the action you wish to take:"
PRINT "1 Open a new data file"
PRINT "2 Add data to the file"
PRINT "3 Close the file and end"''
REPEAT
INPUT TAB(10,20)"What is your choice",choice
UNTIL choice>0 AND choice<4
ON choice PROC_open,PROC_add,PROC_close
.....etc
Code: Select all
90 ON Selection% PROC1, PROC2
Last edited by Zaphod on Fri 29 Jun 2018, 15:55, edited 1 time in total.
-
- Posts: 25
- Joined: Sat 02 Jun 2018, 08:02
Re: Calling a Procedure using a Variable
Hi Patrick,
WOW! Thank you so much!
I have been going round in circles with this, and you've solved it.
Perfect!
Many thanks to you.
John
WOW! Thank you so much!
I have been going round in circles with this, and you've solved it.
Perfect!
Many thanks to you.
John