News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

A thought on procedures

Started by JohnB, Apr 29, 2025, 08:53 AM

Previous topic - Next topic

JohnB

When passing parameters in a procedure could we have a default value to be applied if not passed in the call. A bit like preassigned value for Dim statements.

 e.g. Proc MyProc(param0 as byte, param1 As byte, param2 As Byte = 0)
 
This would allow procedure calls with variable number of parameters.
JohnB

RGV250

Hi John,
I am not sure if I understand, if you are passing a parameter it will already have a value when the procedure is called so would make the default value irrelevant. Would this be optional, if not what happens when you have a procedure without any parameters?

Regards,
Bob

Stephen Moss

Quote from: RGV250 on Apr 29, 2025, 11:59 AMHi John,
I am not sure if I understand, if you are passing a parameter it will already have a value when the procedure is called so would make the default value irrelevant. Would this be optional, if not what happens when you have a procedure without any parameters?

Regards,
Bob

I presume he is thinkings of something similar to a function that exits in visual Basic, in VB if you define a subroutine as...
Friend Async Function Load_File(ByVal Load_Directory As String, ByVal Load_FileName As String) It should be called with something like...
Call Load_File("C:\", TestFile.txt)If you call it without passing both relevant parameters then usually an error would occur, although in VB the error checking should catch it and not allow the code to execute instead flagging the error.

But you can also define a suberoutine like this...
Friend Async Function Load_File(ByVal Load_Directory As String, ByVal Load_FileName As String, Optional ByVal Obfuscate As Integer = 7)In this case you still have to pass the Load_Directory & Load_FileName parameters, however as the Obfuscate is option it can either be passed or omitted, in the latter case Obfuscate will be set to its specified default value of 7.

Thus you can call it with either...
Call Load_File("C:\", TestFile.txt, 2)passing all three parameters (Obfuscate = 2) or...
Call Load_File("C:\", TestFile.txt,)leaving the last (optional) parameter blank (Obfuscate = 7).

The possible downside of not explicitly passing all three parameters is that it may make the code a little harder to follow when you revisit it a few years down the line.

JohnB

Well explained, I was about to make a similar reply..
JohnB

trastikata

Quote from: JohnB on Apr 29, 2025, 08:53 AMe.g. Proc MyProc(param0 as byte, param1 As byte, param2 As Byte = 0)
 
This would allow procedure calls with variable number of parameters.

You can pre-assign a parameter by aliasing it to a variable like the code down but then I can not think of a mechanism how the compiler can tell which parameter is missing when the procedure is called and you omit that parameter - the first, the second or the third? Then if you omit it all the time - simply integrate it in the procedure. Or maybe if you want to modify it from time to time you can still use it as shown and do the parameter modification when needed. Maybe this will help you:

Device = 18F14K50
Declare Xtal = 4

Dim param1 As Byte = 12

Main:
    'Do something
    MyProc(10,param1,14) 'Should return 2
    'Do something
    param1 = 24
    'Do something
    MyProc(10,param1,14) 'Should return 1
    End

Proc MyProc(param0 As Byte, param1 As param1, param2 As Byte), SWord
    Result = (param0 + param2) / param1
EndProc

kcsl

Quotebut then I can not think of a mechanism how the compiler can tell which parameter is missing when the procedure is called and you omit that parameter

Typically the optional parameters have to be at the end of the procedure definition.
So you cannot have this:
Proc Fred(optional param1 as byte = 1, param2 as byte, param3 as byte)

But you could have this:
Proc Fred(param2 as byte, param3 as byte, optional param4 as byte = 4, optional param5 as byte = 2)

There's no room for optimism in software or hardware engineering.

tumbleweed

In most cases all of this is just syntactic sugar for the benefit of the person writing the code, in that it just lets you skip specifying the optional parameters.

Under the hood the code generator always provides all of the parameters every time the procedure is called so there's never any difference in the amount of code produced.

To address the original post, it doesn't really let you use a variable number of parameters.

keytapper

Like other languages, the optional parameter are position dependent, it they aren't passed in by the caller then a values is assigned as default.
Ignorance comes with a cost