Is it possible to make optional procedure parameters as in GreatCowBASIC!

Started by atomix, Feb 17, 2023, 09:02 AM

Previous topic - Next topic

atomix

I really liked how it was done - http://gcbasic.sourceforge.net

#CHIP 18f46k22

Dim var1 as word
Dim var2 as word

' ----- Main body of program commences here.
Do Forever

    var1 = Test()
    var2 = Test(54321)

Loop

Function Test(Optional prm1 as word = 12345) as word
    prm1++
    return prm1
End Function

ASM output:
;Program compiled by Great Cow BASIC (1.00.00 2023-01-01 (Windows 64 bit) : Build 1213) for Microchip MPASM/MPLAB-X Assembler using FreeBASIC 1.07.1/2023-01-02 CRC64
;Need help?
;  See the GCBASIC forums at http://sourceforge.net/projects/gcbasic/forums,
;  Check the documentation and Help at http://gcbasic.sourceforge.net/help/,
;or, email us:
;   w_cholmondeley at users dot sourceforge dot net
;   evanvennn at users dot sourceforge dot net

;********************************************************************************

;Set up the assembler options (Chip type, clock source, other bits and pieces)
 LIST p=18F46K22, r=DEC
#include <P18F46K22.inc>
 CONFIG WRTD = OFF, WRTB = OFF, CPD = OFF, XINST = OFF, LVP = OFF, MCLRE = INTMCLR, WDTEN = OFF, FCMEN = ON, FOSC = INTIO67

;********************************************************************************

;Set aside memory locations for variables
PRM1                             EQU       1          ; 0x1
PRM1_H                           EQU       2          ; 0x2
TEST                             EQU       3          ; 0x3
TEST_H                           EQU       4          ; 0x4
VAR1                             EQU       5          ; 0x5
VAR1_H                           EQU       6          ; 0x6
VAR2                             EQU       7          ; 0x7
VAR2_H                           EQU       8          ; 0x8

;********************************************************************************

;Vectors
    ORG    0
    goto    BASPROGRAMSTART
    ORG    8
    retfie

;********************************************************************************

;Start of program memory page 0
    ORG    12
BASPROGRAMSTART
;Call initialisation routines
    rcall    INITSYS

;Start of the main program
SysDoLoop_S1
    movlw    57
    movwf    PRM1,ACCESS
    movlw    48
    movwf    PRM1_H,ACCESS
    rcall    FN_TEST
    movff    TEST,VAR1
    movff    TEST_H,VAR1_H
    movlw    49
    movwf    PRM1,ACCESS
    movlw    212
    movwf    PRM1_H,ACCESS
    rcall    FN_TEST
    movff    TEST,VAR2
    movff    TEST_H,VAR2_H
    bra    SysDoLoop_S1
SysDoLoop_E1
BASPROGRAMEND
    sleep
    bra    BASPROGRAMEND

;********************************************************************************

INITSYS
;asm showdebug This code block sets the internal oscillator to ChipMHz
;asm showdebug OSCCON type is 104' NoBit(SPLLEN) And NoBit(IRCF3) Or Bit(INTSRC)) and ifdef Bit(HFIOFS)
    bsf    OSCCON,IRCF2,ACCESS
    bsf    OSCCON,IRCF1,ACCESS
    bsf    OSCCON,IRCF0,ACCESS
    bsf    OSCTUNE,PLLEN,ACCESS
;asm showdebug _Complete_the_chip_setup_of_BSR,ADCs,ANSEL_and_other_key_setup_registers_or_register_bits
    clrf    BSR,ACCESS
    clrf    TBLPTRU,ACCESS
    bcf    ADCON2,ADFM,ACCESS
    bcf    ADCON0,ADON,ACCESS
    banksel    ANSELA
    clrf    ANSELA,BANKED
    clrf    ANSELB,BANKED
    clrf    ANSELC,BANKED
    clrf    ANSELD,BANKED
    clrf    ANSELE,BANKED
    bcf    CM2CON,C2ON,ACCESS
    bcf    CM1CON,C1ON,ACCESS
    clrf    PORTA,ACCESS
    clrf    PORTB,ACCESS
    clrf    PORTC,ACCESS
    clrf    PORTD,ACCESS
    clrf    PORTE,ACCESS
    banksel    0
    return

;********************************************************************************

FN_TEST
    incf    PRM1,F,ACCESS
    btfsc    STATUS,Z,ACCESS
    incf    PRM1_H,F,ACCESS
    movff    PRM1,TEST
    movff    PRM1_H,TEST_H
    return

;********************************************************************************


 END

optional.jpg

RGV250

Hi Atomix,
Am I missing something as that is what proceedures do?

Bob

Yasin

Quote from: RGV250 on Feb 17, 2023, 10:28 AM...
Am I missing something as that is what proceedures do?

Bob

Hello every body.

What is intended here. If parentheses are left blank. Default value loading.

JohnB

I think what OP is looking for is:

proc MyProc(param1 as byte, param2 as byte = 42)

where parameter 2 has a default value so you only need pass param1 unless you want to change the default value of param2.
JohnB

Gamboa

Hi,

It's clearer to me as Positron works. it's much neater in this case.


Dim var1 as word
Dim var2 as word
Symbol VarDefault = 12345

' ----- Main body of program commences here.
Do

    var1 = Test(VarDefault)
    var2 = Test(54321)

Loop

Proc Test(prm1 as word), word
    Inc prm1
    Result = prm1
EndProc

Regards,
Gamboa
Long live for you

keytapper

I think the higher the programming level the more complex become.
Ignorance comes with a cost

top204

This is something I was looking into. However, In My Opinion, it makes the code harder to read and understand because some procedure calls will require X amount of parameters and the same procedure will, sometimes, require X + 1 parameters. Making the code harder to follow.

I am looking into "overloading" of procedures, so this will counter-act that mechanism in future versions.

One thing I have noticed with humans over time, and history, and I do not mean this with any disrespect... No matter what a human has, it always wants more, regardless whether the more is required or not, and in most cases, the "more" actually makes things worse for all. :-) Just look at computing over the past couple of decades? In the 1980s and early 1990s, things were quite balanced, then an explosion of nonsense happened and the computing scene is now in a terrible mess with bloat and over-complex languages that are actually less efficient than languages 30 years ago! If the same methodology was maintained with computing now as it was in the 1980s and early 1990s, (efficient programming with what was available) computers would be extremely efficient and programs would be 100 times better and faster.

John Lawton

I tend to agree. Do you remember the PC clones where you could switch the processor clock frequency between Turbo and Normal (4.77MHz)?

There were even utilities that would appear to slow the processor down for compatibility reasons with certain user software.

I sometimes muse that today's PC developers should test their creations with much slower processors, or maybe simulate a slow processor and see how well they run. Then they might want to do a bit more optimisation of their code when they see how badly they run as a result :)

ricardourio

Les, I think any new feature created is a door to open, and then you can see a new world after that, and maybe new doors. It can expand to many things, like the projects we've done with Proton. I'm sure my colleagues here have thought of a possible new application for a project created for a specific request. But for some, these new ports mean profit opportunities, like new programming languages.

Ricardo Urio

Ivano

Quote from: top204 on Feb 20, 2023, 10:50 AMIn the 1980s and early 1990s, things were quite balanced, then an explosion of nonsense happened, and the computing scene is now in a terrible mess with bloat and over-complex languages that are actually less efficient than languages 30 years ago!
You are right, the C64 with a 1 mhz clock and less than 40 kB of memory did extraordinary things for that time. In my opinion, with current technology, PCs should do much more in proportion. Sometimes I wonder where the resources of today's PCs are being wasted.

RGV250

QuoteSometimes I wonder where the resources of today's PCs are being wasted
The programs look prettier, thats about it.
Games are probably the only thing that uses the extra power but in my mind they should be left to a dedicated console.
As for techonlogy taking us backwards, (could be controvertial here) look at all the sheep on TikTok, Facebook, Twitter and the like.

Bob

Frizie

Also applies to the operating systems  :'(
Windows XP is the best Windows version.
To a slightly lesser extent Windows 7.
I've had Windows 11 for six months now, terrible, bursting with errors  >:(
I still use XP for PCB design.
It's a relief how easy and fast XP works  :P
Ohm sweet Ohm | www.picbasic.nl