Start from a main routine: Difference between revisions
(→{{header|Gambas}}: Added PureBasic) |
m (→{{header|PureBasic}}: Descriptive change) |
||
Line 28: | Line 28: | ||
* Right click the MMain module, then select Startup class from the context menu |
* Right click the MMain module, then select Startup class from the context menu |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
PureBasic is procedural and any code which is not part of a procedure is considered 'main' code. This code also does not use any explicit syntax (i.e. a 'main' module) to cause it to execute. |
PureBasic is procedural and any code which is not part of a procedure is considered 'main' code. This code also does not use any explicit syntax (i.e. a 'main' module) to cause it to execute and it always executes first. |
||
=={{header|sed}}== |
=={{header|sed}}== |
Revision as of 18:59, 12 July 2011
Some languages (like Gambas and Visual Basic) support two startup modes. Applications written in these languages start with an open window that waits for events, and it is necessary to do some trickery to cause a main procedure to run instead. Data driven or event driven languages may also require similar trickery to force a startup procedure to run.
The task is to demonstrate the steps involved in causing the application to run a main procedure, rather than an event driven window at startup.
Languages that always run from main() can be omitted from this task.
AWK
The awk language is data driven. However, it does support the use of begin blocks, so we could use one of those to provide us with a main startup procedure:
<lang awk>BEGIN {
# This is our main startup procedure print "Hello World!"
}</lang>
Gambas
In Gambas, to make an application startup from a main routine:
- Create a new module called MMain
- In the MMain module, create a public sub called Main as follows:
<lang gambas>PUBLIC SUB Main()
' This is the start of the program
END</lang>
- Right click the MMain module, then select Startup class from the context menu
PureBasic
PureBasic is procedural and any code which is not part of a procedure is considered 'main' code. This code also does not use any explicit syntax (i.e. a 'main' module) to cause it to execute and it always executes first.
sed
A sed
program repeats itself for each line of input, but your program can begin with commands that address line 1. (This requires that your input has a line 1. If your input is empty file, like /dev/null
, then it is impossible to run commands.)
<lang sed># This code runs only for line 1. 1 { i\ Explain-a-lot processed this file and i\ replaced every period with three exclamation points!!! i\
}
- This code runs for each line of input.
s/\./!!!/g</lang>
Visual Basic
In Visual Basic to make an application startup from a main routine:
- Create a new module called MMain.bas
- Create a new subroutine in the new module as follows:
<lang vb>SUB Main()
' This is the start of the program
END</lang>
- From the menu in the application development environment, choose: File, Project Options.
- Ensure that MMain.bas is selected, by clicking in the list
- From the pulldown list, choose "Sub Main"
- Click the OK button.
ZX Spectrum Basic
On the ZX Spectrum, there is no main function as such. However a saved program can be made to start running from a particular line number by providing the line number as a parameter to the save command. The following example will save the program in memory so that it starts running from line 500:
<lang zxbasic>SAVE "MYPROG" LINE 500: REM For a program with main code starting at line 500</lang>