News:

;) This forum is the property of Proton software developers

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.