Global Local

From Macros Wiki
Revision as of 22:34, 28 July 2010 by ConnerO (talk | contribs)
Jump to navigationJump to search

! show the use of local and global variables and
! the passing of arguments to sub macros

! it also creates a file with the results and
! then displays them when it has finished

a1$ = "This macro shows the use of local and global variables"
a2$ = "and the passing of arguments to other macros"

message "%a1$\n%a2$"

global inum, iunit, a1$, a2$, a3$, a4$
local i, result

! open a file for the results
! ---------------------------
errmac$ = "GlobalLocal_FileError.mac"
file_open 1, "temp.txt", write
if FileError <> 0 then call errmac$, "Error opening file for read", 0

inum = 4

for i = 2 to inum

a1$ = "Call Sub Macro, i = "
a2$ = format$(i,"#0")


file_write_text 1, a1$+a2$
if FileError <> 0 then call errmac$, "Error writing file", 1


call "GlobalLocal_Sub.mac", i, result
if FileError <> 0 then call errmac$, "Error writing file", 1


a1$ = "Back in main Macro, i = "
a2$ = format$(i,"#0")
a3$ = " result = "
a4$ = format$(result, "###0")


file_write_text 1, a1$+a2$+a3$+a4$
if FileError <> 0 then call errmac$, "Error writing file", 1

next i

file_close 1
if FileError <> 0 then call errmac$, "Error closing file", 0

file_is_open = 0

! reopen the file and read the contents
! -------------------------------------

file_open 1, "temp.txt", read
if FileError <> 0 then call errmac$, "Error re-opening file", 0

file_is_open = 1

a1$ = ""
inum = 0

:read
file_read_text 1, a2$
if FileError <> 0 then goto close  ! end of file

a1$ = a1$+a2$+"\n"

inum = inum + 1
if inum < 20 then goto read  ! read a max of 20 lines

:close
file_close 1
if FileError <> 0 then call errmac$, "Error closing file", 0

file_is_open = 0

! delete the temp file
! --------------------

file_delete "temp.txt"
if FileError <> 0 then call errmac$, "Error deleting file", 0

message a1$, "Results"