Using INCLUDES, FUNCTIONS PROCEDURES and SUBROUTINES in Oshonsoft

Thread Starter

camerart

Joined Feb 25, 2013
3,831
But sometimes, using different variables IS MORE confusing. There are times when ONE variable is the proper choice.

I’m going to describe an example. Let’s say your code is dealing with another device. Maybe an external GPS device. Your code has two or more functions that access the device. You would create a GLOBAL variable for the device id or address. Then, you would use the same name in all functions and Main to access the external device.

Otherwise, you’d see the separate name and wonder what it’s value is. You’d have to initialize the device address in Main and each function. At least three places. I don’t know but can guarantee you’ll have a problem because you’ll forget to change the value in one of the functions
Hi D,
I started to reply, then fell into a pile of brain spaghetti o_OI imagine that the top of the program where I set the DIMS TRIS plus other stuff from the D/S, let's call this the HEADER (I think this is what E meant) and will go in an INCLUDE, will have the GLOBAL VARIABLES.

If a FUNCTION, PROCEDURE or INCLUDE has VARIABLE, that isn't affected by the MAIN program, then it can be a different name. (As you can see I'm still in Spaghetti mode)

I just tried a test with an INCLUDE, where I searched for something that is in the INCLUDE and it didn't produce? I think this is why I found that I couldn't use them. There's got to be a method, that others use?
C.
 

djsfantasi

Joined Apr 11, 2010
9,237
As I’m not familiar with OSH, I can’t specifically respond to your issues. My earlier response was based on generally accepted programming practices.

But, you didn’t say what and how you were searching for.

You do realize that included code is contained in a completely separate file, don’t you. As I remember you use text editors to modify your code, only bringing them together when you compile or test.

Your editor may not have a search function that works across multiple files. If you were searching for text that only occurs in the included file while editing the Main code, I wouldn’t expect that you would find something in the “included” file.

Text in a comment or definition of a system variable would only occur in the included file.

So, you would only place code in an included file which you are sure is correct. That way, your main program listing isn’t cluttered with tested functions, system variables and global variables. They would contain “tested it; forget it” code.

For example in the Arduino, I have a function to read strings from a file, parse values of global variable (so I can change the program without re-compiling) and set the internal variables to those values.

This is a lot of code. I tested it with a short program (or stub) and once it worked as desired, I INCLUDEd it in my main program. When debugging, I didn’t have to scroll by this working code to see the code I was working on.

This is a good (IMHO) example of using INCLUDE to manage your code.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
As I’m not familiar with OSH, I can’t specifically respond to your issues. My earlier response was based on generally accepted programming practices.

But, you didn’t say what and how you were searching for.

You do realize that included code is contained in a completely separate file, don’t you. As I remember you use text editors to modify your code, only bringing them together when you compile or test.

Your editor may not have a search function that works across multiple files. If you were searching for text that only occurs in the included file while editing the Main code, I wouldn’t expect that you would find something in the “included” file.

Text in a comment or definition of a system variable would only occur in the included file.

So, you would only place code in an included file which you are sure is correct. That way, your main program listing isn’t cluttered with tested functions, system variables and global variables. They would contain “tested it; forget it” code.

For example in the Arduino, I have a function to read strings from a file, parse values of global variable (so I can change the program without re-compiling) and set the internal variables to those values.

This is a lot of code. I tested it with a short program (or stub) and once it worked as desired, I INCLUDEd it in my main program. When debugging, I didn’t have to scroll by this working code to see the code I was working on.

This is a good (IMHO) example of using INCLUDE to manage your code.
Hi D,
Since posting my last, I've spoken to my mate who's helping me.
I wanted to make sure his 'view' like yours which is "generally accepted programming practices." didn't conflict with OSH.
He said the same as you! In that an INCLUDE is forget.
My concern was that if DIMS were in an INCLUDE, then when I want to check them by SEARCH, I wouldn't find them, but it's simple= Don't put them in there.
I'm getting it slowly, thanks.
C
 

djsfantasi

Joined Apr 11, 2010
9,237
Hi D,
Since posting my last, I've spoken to my mate who's helping me.
I wanted to make sure his 'view' like yours which is "generally accepted programming practices." didn't conflict with OSH.
He said the same as you! In that an INCLUDE is forget.
My concern was that if DIMS were in an INCLUDE, then when I want to check them by SEARCH, I wouldn't find them, but it's simple= Don't put them in there.
I'm getting it slowly, thanks.
C
Other options are to use a text editor that can search multiple files. I use Textpad.

Or in your Main program, put in comments that list the arrays and which INCLUDE file contains the definitions.

Just some ideas ...;)
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Other options are to use a text editor that can search multiple files. I use Textpad.

Or in your Main program, put in comments that list the arrays and which INCLUDE file contains the definitions.

Just some ideas ...;)
Hi D,
Thanks.
I now also comment 'RETURN' with GOSUB that was used.
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Hi,
Over the years, I've picked up that GOSUBS are frowned upon.
I tried changing a GOSUB, to a PROCEDURE, which worked! This entailled also moving a DIM from GLOBAL (It think I get this now) into the PROCEDURE, which broke a second, but related PROCEDURE, so this also has that DIM in it.
To complete this PROCEDURE fully, I'm sure lots of related DIMS also need to be moved into it, so I imaging each time this PROCEDURE is CALLED it would take the time to set all of these DIMS. Is this correct?

If so, I thought perhaps all of the DIMS should go into an INCLUDE which I presume is GLOBAL and only CALLED once, which to me seems better.
Am I on the correct path?

Meanwhile, my mate is going through the FULL program, and making a flow chart. By that time, hopefully, I should have answered the initial thread question;)
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Hi,
I've come back to this thread, after quite a while, and haven't looked through it again.

I'm not sure if this matters, but I'm using Oshonsoft.
In regard to PROCEDUREs and FUNCTIONS:
Am I correct that a FUNCTION returns 1x value back into the main program, with VARIABLES that are not changed outside of the FUNCTION, and PROCEDURES change VARIABLES in the main program, external to the PROCEDURE?
C
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Hi,
I program in Oshonsoft BASIC only, and am mildly dyslexic, but I can move forward, but it takes a long time.
I'm just l learning Functions Procedures and Includes.

Regarding Procedures:
Here is the Procedure example:
Code:
Dim in_only As Byte
   Dim inc_me As Byte
   Dim add_inc_me_and_in_only As Byte

   in_only = 5
   inc_me = 10
   Call testbyref(in_only, inc_me, add_inc_me_and_in_only)
   'after this call
   'inc_me = 11
   'add_inc_me_and_in_only = 16
   End

   Proc testbyref(arg1 As Byte, ByRef arg2 As Byte, ByRefOut arg3 As Byte)
      arg2 = arg2 + 1
      arg3 = arg1 + arg2
   End Proc
I have spent a long time trying to understand how it works, and just had it 'translated' into this:
Code:
Dim AA As Byte
Dim BB As Byte
Dim CC As Byte

AA = 5
BB = 10
Call result(AA, BB, CC)
'after this call
'b = 11
'c = 16
End                                            


Proc result(arg1 As Byte, ByRef arg2 As Byte, ByRefOut arg3 As Byte)
arg2 = arg2 + 1
arg3 = arg1 + arg2
End Proc
Much easier now.
I tried to change { ByRef ], but wasn't allowed, so I looked it up and find it means something.
How can anyone learning be expected to know what it means without a link or something. Perhaps it's in the explanation somewhere?

Anyway, a bit of a rant, so that's better :)
Cheers, Camerart
 

jjw

Joined Dec 24, 2013
823
Hi,
I carried on experimenting after the last post, here's the result:

This adds to the confusion!, I
m not sure which to choose for my use?
C.
It depends what you want to do.
In the first BB= BB+1
in the second arg2=BB+1. BB is not changed.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
It depends what you want to do.
In the first BB= BB+1
in the second arg2=BB+1. BB is not changed.
Hi J,
I searched and read the { ByRef ] info but it was a bit too much to understand at the moment.

I'm using it with the Procedures and Functions you wrote on the 'other' thread, as I've got to change my working CODE to your method.
C
 

jjw

Joined Dec 24, 2013
823
You don't have to change your working code.
I just gave an example of using functions.
If you want to change global variables inside a function or procedure it is possible likewise outside function.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
You don't have to change your working code.
I just gave an example of using functions.
If you want to change global variables inside a function or procedure it is possible likewise outside function.
Hi J,
Ok, perhaps I'll leave it till later.
Thanks, C.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Hi,
I think I may understand how a Function works now, here's the latest attempt (At last):)EDIT: I think I've found errors in the program, if so I'll re-post it correctly.
EDIT EDIT: I've removed the files, as incorrect, and gone back to the main thread
More questions later:)
C
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Hi,
I have a program with a working FUNCTION, but it appears that I've got soemething wrong.
It's to do with Global and Local!
I'm sure it's all been explained before, so I won't ask again, but, I'll be unravelling spehetti for a while.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Hi, (Learnt something today):)
The term ARG1/2.. has always jarred with me, because 'what is it?'. I always tried to use 'my variable' where ARG1/2.. is normally used, so that I knew what I was dealing with.

Having just got a program to work with a SECTION that uses the FUNCTION, and think I understand how they work, I started to add the next SECTION in the sequence, that needs the same FUNCTION. I then realised that 'my variable' doesn't work, because it has to change to the new SECTION variable.

So ARG1 would adopt the value of the 1st SECTION when being used, then change to value of the 2nd SECTION value when that is being used.
So now, ARG1 doesn't jar anymore, and I'll use it from now on.

C.
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,831
Hi,
I'm using the same FUNCTION to WRITE to each of 3x peripherals. In the FUNCTION is CHIP SELECT. Each of the peripherals has a different C/S PIN, so which is the best way to change the C/S to the correct peripheral before each time I call the FUNCTION please?
C.
 

ericgibbs

Joined Jan 29, 2010
21,448
hi,
Edited extract from the OSH Manual.

Functions can be declared with FUNCTION statement.
They can contain up to 5 arguments and argument variables are declared locally.

Functions can be exited with EXIT statement and must be ended with END FUNCTION.

The name of the function is declared as a global variable,
so if the function is called with CALL statement, after its execution the function
variable will contain the result. Standard way of function calls in assignment statements can be used, also.
One simple example:
DIM A AS BYTE
DIM B AS WORD
DIM C AS WORD
DIM D AS WORD
DIM E AS BYTE

FOR A = 0 TO 255
Result = Fname(A,B,C,D,1) '' Call Func with upto 5 args [ parameters], say let last arg be the CS pin
NEXT A
END

FUNCTION Fname(arg1 AS WORD,arg2 AS WORD,arg3 AS WORD,arg4 AS BYTE) AS WORD
CS = Arg5
'do what ever in the Function
'Return result as WORD
END FUNCTION
 

Thread Starter

camerart

Joined Feb 25, 2013
3,831
hi,
Edited extract from the OSH Manual.

Functions can be declared with FUNCTION statement.
They can contain up to 5 arguments and argument variables are declared locally.

Functions can be exited with EXIT statement and must be ended with END FUNCTION.

The name of the function is declared as a global variable,
so if the function is called with CALL statement, after its execution the function
variable will contain the result. Standard way of function calls in assignment statements can be used, also.
One simple example:
DIM A AS BYTE
DIM B AS WORD
DIM C AS WORD
DIM D AS WORD
DIM E AS BYTE

FOR A = 0 TO 255
Result = Fname(A,B,C,D,1) '' Call Func with upto 5 args [ parameters], say let last arg be the CS pin
NEXT A
END

FUNCTION Fname(arg1 AS WORD,arg2 AS WORD,arg3 AS WORD,arg4 AS BYTE) AS WORD
CS = Arg5
'do what ever in the Function
'Return result as WORD
END FUNCTION
Hi E,
I think I understand, but as I've got at least 3x peripherals each with different C/S PORT.X, can you tell me where in this method it set's PORTs up please? Also there are 2x other ARGs as well

EDIT: If [ chip_sel = altmtr_cs ] is a ligitimate statement, then I could write a PROCEDURE to select [ chip_sel = altmtr_cs or chip_sel = cmpss_cs or chip_sel = 4431_cs ] and select each as the sequence procedes, and use chip_sel as one of the AGRs. Does this seem ok?
C
 
Last edited:
Top