Start from a main routine: Difference between revisions

Added Easylang
m (→‎{{header|Quackery}}: too many whens!)
(Added Easylang)
 
(20 intermediate revisions by 11 users not shown)
Line 1:
{{task}}
 
{{omit from|BBC BASIC}}
 
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.
Line 10:
Languages that always run from main() can be omitted from this task.
<br><br>
=={{header|6502 Assembly}}==
This depends entirely on the hardware.
* Apple ][: type <code>"BRUN PROG"</code> and press enter
* Commodore 64: type <code>"LOAD"*",8,1</code> and press enter. This is how programs on floppy disks are loaded. Commodore 64 cartridges bypass this step entirely and boot immediately.
* NES/Famicom: Executes <code>JMP ($FFFC)</code> at the start automatically, thus entering the main program.
 
It should be noted that nothing needs to be called "main" but generally speaking the "main" program is whatever execution flows into at the start.
 
=={{header|8086 Assembly}}==
Whether a program runs automatically from main or needs to be called depends entirely on the hardware, but in MS-DOS you have to type the name of the program you wish to run.
<code>C:\>prog.exe</code>
 
=={{header|Ada}}==
Line 15 ⟶ 26:
In Ada, the "Main" procedure doesn't have to follow a special naming scheme. Any parameterless procedure will do.
 
<langsyntaxhighlight lang="ada">with Ada.Text_IO;
procedure Foo is
begin
Ada.Text_IO.Put_Line("Bar");
end Foo;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
An Algol 68 program consists of a sequence of declarations and expressions. The expressionssexpressions are executed and so form the main program.
<langsyntaxhighlight lang="algol68">BEGIN
print( ( "Hello, World!", newline ) )
END</langsyntaxhighlight>
 
=={{header|Arturo}}==
Line 33 ⟶ 44:
If we need a <code>main</code> function in any case, we can of course create it and call it.
 
<langsyntaxhighlight lang="rebol">main: function [][
print "Yes, we are in main!"
]
 
main</langsyntaxhighlight>
 
{{out}}
Line 50 ⟶ 61:
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:
 
<langsyntaxhighlight lang="awk">BEGIN {
# This is our main startup procedure
print "Hello World!"
}</langsyntaxhighlight>
 
=={{header|BASIC}}==
==={{header|GWBASIC}}===
{{works with|GWBASIC}}
As is well known, GWBASIC cannot refer to anything with a name. Also, does not require an executable program to have a main() procedure.
However, as in the other instances, there's nothing to stop you creating a subprocess and then calling it by its line number to start the program:
<syntaxhighlight lang="qbasic">10 'SAVE "MAIN",A
20 GOSUB 100 ' Main
30 END
100 ' Sub Main
110 CLS
120 PRINT "This is the Main sub!"
130 RETURN</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
QBasic does not require an executable program to have a main() procedure.
However, there's nothing to stop you creating one and then calling it to start the program:
<syntaxhighlight lang="qbasic">SUB main
PRINT "Hello from main!"
END SUB
 
main</syntaxhighlight>
 
==={{header|BASIC256}}===
BASIC256 does not require an executable program to have a main() procedure.
However, there's nothing to stop you creating one and then calling it to start the program:
<syntaxhighlight lang="basic256">subroutine main()
print "Hello from main!"
end subroutine
call main()</syntaxhighlight>
 
==={{header|Run BASIC}}===
Run BASIC does not require an executable program to have a main() procedure.
However, there's nothing to stop you creating one and then calling it to start the program:
<syntaxhighlight lang="lb">sub main
print "Hello from main!"
end sub
 
call main</syntaxhighlight>
 
==={{header|True BASIC}}===
{{works with|QBasic}}
True BASIC does not require an executable program to have a main() procedure.
However, there's nothing to stop you creating one and then calling it to start the program:
<syntaxhighlight lang="qbasic">SUB main
PRINT "Hello from main!"
END SUB
 
CALL main
END</syntaxhighlight>
 
==={{header|Yabasic}}===
Yabasic does not require an executable program to have a main() procedure.
However, there's nothing to stop you creating one and then calling it to start the program:
<syntaxhighlight lang="yabasic">sub main()
print "Hello from main!"
end sub
main()</syntaxhighlight>
 
=={{header|C}}==
Line 59 ⟶ 132:
Code execution in C always starts at main(). Macros make possible programs such as the one below, although it's not the best way to write C code.
 
<syntaxhighlight lang="c">
<lang C>
#include<stdio.h>
 
Line 69 ⟶ 142:
return 0;
}
</syntaxhighlight>
</lang>
 
<pre>
Line 80 ⟶ 153:
=={{header|Component Pascal}}==
In BlackBox Componente Builder any exported procedure without paramenter (command) can be invoked through commanders (CTRL-q ModuleName.command)
<langsyntaxhighlight lang="oberon2">
MODULE MainProcedure;
IMPORT StdLog;
Line 95 ⟶ 168:
END MainProcedure.
 
</syntaxhighlight>
</lang>
Execute:<br/>
^Q MainProcedure.Do<br/>
Line 104 ⟶ 177:
From Main
</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc main . .
print "Hello from main!"
.
main
</syntaxhighlight>
 
=={{header|Erlang}}==
Line 110 ⟶ 191:
=={{header|Factor}}==
Use <code>MAIN:</code> to specify the starting point of a runnable vocabulary.
<langsyntaxhighlight lang="factor">USE: io
IN: example
 
: hello ( -- ) "Hello, world!" print ;
 
MAIN: hello</langsyntaxhighlight>
 
This is optional for scripts and interactive development, but required to deploy a Factor program as an executable binary.
Line 121 ⟶ 202:
=={{header|Forth}}==
Forth still runs the interpreter when given a file to include in order to compile the source, but you can avoid the interactive interpreter by invoking an entry point ("main") then calling BYE to exit.
<langsyntaxhighlight lang="forth">include foo.fs
...
: main ... ;
 
main bye</langsyntaxhighlight>
 
This pattern is also used (e.g. GNU Forth) to interpret a Forth snippet from the command line.
 
<langsyntaxhighlight lang="forth">$ gforth -e "2 2 + . bye"</langsyntaxhighlight>
 
Furthermore, professional Forth systems like PFE and SwiftForth have a means to make a "turnkey" application which omits the interactive interpreter, suitable for installing on a third-party system. The command would take an entry point and target executable name:
 
<syntaxhighlight lang ="forth">' main turnkey app.exe</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
FreeBASIC does not require an executable program to have a main() procedure. However, there's nothing to stop you creating one and then calling it to start the program:
 
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Sub main()
Line 145 ⟶ 226:
 
main
Sleep</langsyntaxhighlight>
 
{{out}}
Line 160 ⟶ 241:
* In the MMain module, create a public sub called Main as follows:
 
<langsyntaxhighlight lang="gambas">PUBLIC SUB Main()
' This is the start of the program
END</langsyntaxhighlight>
 
* Right click the MMain module, then select Startup class from the context menu
Line 174 ⟶ 255:
 
In practice, init() functions are generally used to initialize top-level variables which cannot (or cannot easily) be initialized in situ rather than to pre-empt the main() function in this way.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 197 ⟶ 278:
count++
fmt.Println("main called when count is", count)
}</langsyntaxhighlight>
 
{{out}}
Line 217 ⟶ 298:
 
If you want the script to exit even when it hits an error, you can use an [http://www.jsoftware.com/help/dictionary/dx009.htm#26 immex phrase], which will be the first thing executed by the event loop, before it prompts.
 
=={{header|jq}}==
 
A jq program consists of a (possibly empty) sequence of directives and function definitions followed by a single (but possibly compound) jq expression that is evaluated in the context of the prior definitions. The jq expression can take many forms, such as a single JSON value, or a pipeline of filters. In particular, it could be the invocation of a previously defined function named main as in this example:
<syntaxhighlight lang="jq">
def main:
"Hello, World!";
main
</syntaxhighlight>
 
The jq interpreter has two modes of operation: a data-driven mode (the default) and an autonomous mode (corresponding to the `-n` command-line option).
In general, in the data-driven mode, the expression at the end of the jq program is evaluated once for each input, so that in the above example,
the "Hello, World!" string would be emitted once for each input.
By contrast, if the `-n` command-line option is specified, then a single JSON `null` would be presented to the final expression, so that in the above example,
the string would be emitted just once.
 
=={{header|Julia}}==
Line 235 ⟶ 332:
=={{header|Logtalk}}==
Logtalk applications don't run a main procedure at startup by default but there are several ways to accomplish that. One of them is to include an initialization/1 in a source file. The argument of this directive is a goal that will be proved when the source file is compiled and loaded. For example:
<langsyntaxhighlight lang="logtalk">
:- initialization(main).
</syntaxhighlight>
</lang>
The initialization/1 can also be used within entities (objects and categories) to automatically run entity-specific initializations when the container source file is compiled and loaded. In alternative, it's usually possible to pass a goal as a command-line argument when running the application executable (the details depending on the backend Prolog compiler being used).
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Mathematica automatically starts a REPL (read-eval-print loop), which is a kind of event loop. If that is not desired, pass -run on the command line. Note that if Mathematica is called via MathLink from within an external program, then the main loop has to be defined, which will usually differ from the standard one.
 
Line 257 ⟶ 354:
 
For instance if file1.of is :
<langsyntaxhighlight Oforthlang="oforth">: main(n)
"Sleeping..." println
n sleep
"Awake and leaving." println ;</langsyntaxhighlight>
 
{{out}}
Line 271 ⟶ 368:
 
With this file (for instance file2.of) :
<langsyntaxhighlight Oforthlang="oforth">: main(n)
"Sleeping..." println
n sleep
"Awake and leaving." println ;
 
10000 main</langsyntaxhighlight>
 
{{out}}
Line 305 ⟶ 402:
=={{header|Perl}}==
Same as Raku.
<langsyntaxhighlight lang="perl">BEGIN {...} # as soon as parsed
CHECK {...} # end of compile time
INIT {...} # beginning of run time
END {...} # end of run time</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
Any code which is not part of a routine is considered 'main' code. If you want a <code>main()</code> you have to explicitly invoke it.
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #0000FF;">...</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
Line 326 ⟶ 423:
By default, Processing executes the given list of commands with no required main function/method. This is called "immediate mode."
 
<langsyntaxhighlight Processinglang="processing">println("hello world");
line(0, 0, width, height);</langsyntaxhighlight>
 
Most of the Processing API can be used in immediate mode--however, it has no event loop, so it cannot support animation, interaction (such as keyboard or mouse) or time-based media such as audio or video. Further, this mode cannot define new functions/methods, or classes. For example, this is invalid:
 
<langsyntaxhighlight Processinglang="processing">void hello() {
println("hello world");
}
hello();</langsyntaxhighlight>
 
In order to define functions or use these language features, setup() and/or draw() must be defined in a Processing sketch. These serve as the equivalent of a main() entrypoint. For example, this is a valid sketch:
 
<langsyntaxhighlight Processinglang="processing">void setup() {
hello();
}
void hello() {
println("hello world");
}</langsyntaxhighlight>
 
setup() -- or draw() if there is no setup() -- acts as an entrypoint to the sketch, with draw() also serving as an event loop that by default is called at 60fps.
Line 370 ⟶ 467:
Other variations are possible. To illustrate this, here is a program which, if typed in in the shell, or included in a <code>.qky</code> file, will temporarily suspend compilation to interact with the user. <code>constant</code> includes the functionality of <code>now!</code> but treats the top item of the stack as if it were part of the program text.
 
<langsyntaxhighlight Quackerylang="quackery">[ say "Please use the shell to calculate a value" cr
say "and leave it on the stack. Type 'leave' when" cr
say "you have done this." cr
shell ] constant is my-value</langsyntaxhighlight>
 
Demonstrating this in the Quackery shell. Text after the duck's head prompt and the continuation ellipses is entered by the user.
Line 420 ⟶ 517:
Racket can be configured to run a REPL, run a main function, or just run top-level expressions. A <tt>main</tt> function can be run by executing <tt>racket -tm program.rkt</tt>.
 
<langsyntaxhighlight lang="racket">
#/usr/bin/env racket -tm
#lang racket
(provide main)
(define (main . args) (displayln "Hello World!"))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 431 ⟶ 528:
When executed with the standard setting, Raku code always runs the mainline code automatically, followed by the <tt>MAIN</tt> function if you have one. However, it's possible to start up with an alternate setting that might want to create its own event loop or <tt>MAIN</tt>. In such cases you can
always capture control at various phases with blocks that we call "phasers":
<syntaxhighlight lang="raku" perl6line>BEGIN {...} # as soon as parsed
CHECK {...} # end of compile time
INIT {...} # beginning of run time
END {...} # end of run time</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 442 ⟶ 539:
<br>XEDIT has native (built-in) support for the REXX language as a macro language.
<br>The XEDIT mentioned above runs on the VM/CMS operating system.
<langsyntaxhighlight lang="rexx">/*REXX*/
address 'XEDIT'
.
Line 450 ⟶ 547:
.
.
.</langsyntaxhighlight>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
func Main
see "Hello World!" + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 465 ⟶ 562:
Every Ruby source file can declare blocks of code to be run as the file is being loaded (the BEGIN blocks) and after the program has finished executing (the END blocks).
[http://ruby-doc.com/docs/ProgrammingRuby/html/language.html#UA BEGIN and END Blocks]
<langsyntaxhighlight lang="ruby">BEGIN {
# begin code
}
Line 471 ⟶ 568:
END {
# end code
}</langsyntaxhighlight>
A program may include multiple BEGIN and END blocks. BEGIN blocks are executed in the order they are encountered. END blocks are executed in reverse order.
 
Line 478 ⟶ 575:
===No kidding and trickery===
In Scala there are two concepts not available in another OO-languages e.g. Java. The concepts are <code>object</code> and <code>trait</code>. Both cannot have parameters. <code>object</code>'s are singletons (one and only one instance of a parameter-less class) and are static. Exactly the same as a <code>main</code>. By use of the <code>trait App</code> a main method is preprogrammed and brought in an <code>object</code> which can be called on the command-line. There are more the one different <code>object</code>'s possible each can be called by his object name on the command-line. In the trait <code>executionStart</code> field is initialized with the starting time. By submitting an <code>-Dscala.time</code> argument on the command-line the execution time can be reported. The field <code>executionStart</code> can also programmatically used.
{{out|Example}}<langsyntaxhighlight lang="scala">object PrimaryMain extends App {
Console.println("Hello World: " + (args mkString ", "))
}
Line 484 ⟶ 581:
object MainTheSecond extends App {
Console.println("Goodbye, World: " + (args mkString ", "))
}</langsyntaxhighlight>
 
=={{header|sed}}==
A <code>sed</code> 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 <code>/dev/null</code>, then it is impossible to run commands.)<langsyntaxhighlight lang="sed"># This code runs only for line 1.
1 {
i\
Line 498 ⟶ 595:
 
# This code runs for each line of input.
s/\./!!!/g</langsyntaxhighlight>
 
=={{header|Seed7}}==
Code execution in Seed7 always starts with main. This holds for text programs:
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
begin
writeln("hello world");
end func;</langsyntaxhighlight>
 
And for grahical programs:
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "draw.s7i";
include "keybd.s7i";
Line 521 ⟶ 618:
KEYBOARD := GRAPH_KEYBOARD;
ignore(getc(KEYBOARD));
end func;</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 534 ⟶ 631:
* Create a new subroutine in the new module as follows:
 
<langsyntaxhighlight lang="vb">SUB Main()
' This is the start of the program
END</langsyntaxhighlight>
 
* From the menu in the application development environment, choose: File, Project Options.
Line 556 ⟶ 653:
OnInitialize can cancel startup by returning false, and the handler for Startup can cancel startup by setting e.Cancel to True.
 
<langsyntaxhighlight lang="vbnet">Imports System.Collections.ObjectModel
Imports Microsoft.VisualBasic.ApplicationServices
 
Line 590 ⟶ 687:
End Sub
End Class
End Namespace</langsyntaxhighlight>
 
{{out|input=foo /bar baz "visual basic"}}
Line 617 ⟶ 714:
Main can choose to not call Application.Run, in which case no form is created.
 
<langsyntaxhighlight lang="vbnet">
Module Main
Sub Main(args As String())
Line 623 ⟶ 720:
Application.Run(New Form1())
End Sub
End Module</langsyntaxhighlight>
 
{{out|input=foo /bar baz "visual basic"}}
<pre>main; args:foo, /bar, baz, visual basic</pre>
 
=={{header|V (Vlang)}}==
main() is the usual entry point for a program.
<syntaxhighlight lang="Vlang">
 
fn main() {
println("hello world")
}
</syntaxhighlight>
 
However, this can be skipped in one file programs, allowing Vlang to be used like a script.
<syntaxhighlight lang="Vlang">
println("hello world")
</syntaxhighlight>
 
=={{header|Wren}}==
Line 632 ⟶ 743:
 
However, it's easy enough to create a ''main'' function and call that to start execution of the script.
<langsyntaxhighlight ecmascriptlang="wren">var main = Fn.new {
System.print("Hello from the main function.")
}
 
main.call()</langsyntaxhighlight>
 
{{out}}
<pre>
Hello from the main function.
</pre>
 
=={{header|XBasic}}==
Applications in XBasic always start from a Sub Main called <code>FUNCTION Entry()</code>.
Nevertheless, to make an application start from a main routine:
* Create a new module called MMain
* Create a new function in the new module as follows:
 
<syntaxhighlight lang="xbasic">FUNCTION main ()
'This is the beginning of the program
END FUNCTION</syntaxhighlight>
 
Press F1 to start the application
<syntaxhighlight lang="xbasic">PROGRAM "Start from a main routine"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION main ()
 
FUNCTION Entry ()
main ()
END FUNCTION
 
FUNCTION main ()
PRINT "Hello from main!"
END FUNCTION
 
END PROGRAM</syntaxhighlight>
 
=={{header|XPL0}}==
XPL0 programs are normally compiled and run from a main procedure, although this procedure is not explicitly named 'main'. It is possible to compile procedures without a main procedure that are later linked to a module with a main procedure. This feature was used frequently on old, slow 6502-based computers, such as the Apple II.
 
=={{header|Z80 Assembly}}==
This depends entirely on the hardware. WinAPE (an Amstrad CPC emulator) can run a program by typing <code>call &addr</code> where <code>addr</code> is the operand of the <code>org</code> directive at the beginning of the source document.
 
In your source code:
<syntaxhighlight lang="z80">org &8000</syntaxhighlight>
 
At the Amstrad CPC terminal, type this:
<pre>
call &8000
</pre>
 
Line 647 ⟶ 798:
 
file foo.zkl:
<langsyntaxhighlight lang="zkl">"Hello".println()</langsyntaxhighlight>
forms an anonymous class whose constructor is a function with the above code. "zkl foo" does the expected.
 
Line 654 ⟶ 805:
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:
 
<langsyntaxhighlight lang="zxbasic">SAVE "MYPROG" LINE 500: REM For a program with main code starting at line 500</langsyntaxhighlight>
 
{{omit from|Axe}}
{{omit from|BBC BASIC}}
{{omit from|Bc|Scripts start running from the top}}
{{omit from|Brlcad|Scripts start running from the top in mged}}
Line 668 ⟶ 820:
{{omit from|Java|Uses a main method}}
{{omit from|NetRexx|Uses a main method}}
{{omit from|Plain English|Programs start from a 'run' method}}
{{omit from|Python}}
{{omit from|Rust}}
{{omit from|Swift}}
{{omit from|UNIX Shell|Scripts start running from the top}}
{{omit from|XPL0}}
{{omit from|zkl}}
 
2,046

edits