はじめに
#include <stdio.h>
int hello(void); /* 関数プロトタイプのこの行を削除するとコンパイル時に警告 */
int main(void)
{
hello();
return 0;
}
int hello(void)
{
puts("Hello World!");
return 0;
}
や
program hello;
procedure hello; forward; (* 前方宣言のこの行を削除するとコンパイルエラー *)
procedure main;
begin
hello
end;
procedure hello;
begin
writeln('Hello World!')
end;
begin
main
end.
のように、前方で定義された関数や手続きを安全に呼び出すにはそれなりの記述を必要とする言語があり、そうでないものも存在する。Qiita の某記事のコメント欄で前方参照宣言等なしに前方で定義した関数等を呼び出せる(ように見える)言語等をいくつか挙げてみたらそこそこの数になってきたのでいい加減記事主にも迷惑だろうと引っ越しして記事化。
なお、
#include <stdio.h>
#ifndef INCLUDED
#define INCLUDED
#include __FILE__
int main(void)
{
hello();
return 0;
}
#else
int hello(void)
{
puts("Hello World!");
return 0;
}
#endif
のようなトンチ回答は対象としない。
実際よくわからんところで書いてるので内容は多くの人の参考になるものではあるまい。
##AWK
BEGIN {
hello() # OK
}
function hello() {
print "Hello World!"
}
##Perl
&hello(); # OK
sub hello() {
print "Hello World!"
}
##Ruby
def main()
hello() # OK
end
def hello()
puts 'Hello World!'
end
main()
##Python
def main():
hello() # OK
def hello():
print('Hello World!')
main()
##PHP
<?php
hello(); // OK
function hello()
{
echo "Hello World!\n";
}
?>
##Dart
void main() {
hello(); // OK
}
void hello() {
print('Hello World!');
}
hello(); // OK
function hello() {
console.log("Hello World!");
}
main = ->
hello() # OK
return
hello = ->
console.log "Hello World!"
return
main()
##Groovy
class Ideone {
static void main(String[] args) {
Hello.hello() // OK
}
}
class Hello {
static void hello() {
println "Hello World!"
}
}
(defun main ()
(hello)) ; OK
(defun hello ()
(message "Hello World!"))
(main)
function Main()
call Hello() " OK
endfunction
function Hello()
echo "Hello World!"
endfunction
call Main()
##Lua
function main()
hello() -- OK
end
function hello()
print "Hello World!"
end
main()
##Tcl
proc main {} {
hello ;# OK
}
proc hello {} {
puts "Hello World!"
}
main
##CMake
function(main)
hello() # OK
endfunction()
function(hello)
message("Hello World!")
endfunction()
main()
##Make
main: hello # OK
hello:
@echo "Hello World!"
※ 行頭の空白はタブ
##bc
define main() {
dummy = hello() # OK
}
define hello() {
print "Hello World!\n"
}
dummy = main()
##dc
[lh x] sm # OK
[[Hello World!]p] sh
lm x
##Bash
#!/bin/bash
main() {
hello # OK
}
hello() {
echo "Hello World!"
}
main
##Octave
function main
hello # OK
end
function hello
disp "Hello World!"
end
main
(define (main args)
(hello) ; OK
0)
(define (hello)
(display "Hello World!")
(newline))
call hello ; OK
end
:hello
dispstr "Hello World!"#13#10
return
@echo off
call :hello & :: OK
exit/b
:hello
echo Hello World!
exit/b
Function main() {
hello # OK
}
Function hello() {
Write-Output "Hello World!"
}
main
hello() // OK
function hello() {
WScript.Echo("Hello World!")
}
hello ' OK
Function hello()
WScript.Echo "Hello World!"
End Function
##cpp
#define main hello // OK
#define hello Hello World!
main
##m4
divert(-1)
define(main, `hello') # OK
define(hello, `Hello World!')
divert(0)dnl
main
##roff
.de ma
.he \" OK
..
.de he
Hello World!
..
.ma
##TeX
\def \main{\hello} % OK
\def \hello{Hello World!}
\main
\bye
##Rexx
call hello /* OK */
exit
hello: procedure
say 'Hello World!'
return
##D
import std.stdio;
void main()
{
hello(); // OK
}
void hello()
{
writeln("Hello World!");
}
##Go
package main
import "fmt"
func main() {
hello(); // OK
}
func hello() {
fmt.Printf("Hello World!\n")
}
##Rust
fn main() {
hello(); // OK
}
fn hello() {
println!("Hello World!");
}
##Swift
func main() {
hello() // OK
}
func hello() {
print("Hello World!")
}
main()
##C++
#include <iostream>
class hoge {
public:
hoge()
{
hello(); // OK
}
void hello()
{
std::cout << "Hello World!" << std::endl;
}
};
int main()
{
hoge hoge;
}
##Arduino
void setup() {
Serial.begin(9600);
hello(); // OK
}
void loop() {
}
void hello() {
Serial.println("Hello World!");
}
##Java
class Ideone
{
public static void main (String[] args)
{
Hello.hello(); // OK
}
}
class Hello
{
public static void hello()
{
System.out.println("Hello World!");
}
}
##Kotlin
fun main(args: Array<String>) {
hello() // OK
}
fun hello() {
println("Hello World!")
}
##C#
public class Hogera
{
public static void Main()
{
Hello.hello(); // OK
}
}
public class Hello
{
public static void hello()
{
System.Console.WriteLine("Hello World!");
}
}
Public Class Test
Public Shared Sub Main
Hello.hello ' OK
End Sub
End Class
Public Class Hello
Public Shared Sub hello
Console.WriteLine("Hello World!")
End Sub
End Class
##Erlang
-module(prog).
-export([main/0]).
main() ->
hello(). % OK
hello() ->
io:format("Hello World!~n").
##Elixir
defmodule Main do
def main do
Hello.hello # OK
end
end
defmodule Hello do
def hello() do
IO.puts "Hello World!"
end
end
Main.main
(define (main)
(hello)) ; OK
(define (hello)
(display "Hello World!")
(newline))
(main)
(defun main ()
(hello)) ; OK
(defun hello ()
(format t "Hello World!~%"))
(main)
##Clojure
(defn -main []
(eval (read-string "(hello)"))) ; OK
(defn hello []
(println "Hello World!"))
(-main)
##Haskell
main = hello -- OK
hello = putStrLn "Hello World!"
##Scala
object Main {
def main(args: Array[String]) {
Hello.hello() // OK
}
}
object Hello {
def hello() {
printf("Hello World!\n")
}
}
##Nemerle
class Main {
static Main() : void {
Hello.hello(); // OK
}
}
class Hello {
public static hello() : void {
System.Console.WriteLine("Hello World!");
}
}
##Nice
void main(String[] args)
{
hello(); // OK
}
void hello()
{
System.out.println("Hello World!");
}
##Fantom
class FantomSay {
Void main(Str[] args) {
Hello.hello() // OK
}
}
class Hello {
static Void hello() {
echo("Hello World!")
}
}
##Icon
procedure main()
hello() # OK
end
procedure hello()
write("Hello World!")
end
##Pike
int main() {
hello(); // OK
return 0;
}
void hello() {
write("Hello World!\n");
}
##Gosu
hello() // OK
private function hello() : void {
print("Hello World!")
}
##Prolog
:- initialization(main).
main :- hello, % OK
halt.
hello :- write('Hello World!'), nl.
##Crystal
hello # OK
def hello()
puts "Hello World!"
end
##Pony
actor Main
new create(env: Env) =>
Hello.hello(env) /// OK
actor Hello
be hello(env: Env) =>
env.out.print("Hello World!")
##Rill
import std.stdio;
def main() {
hello(); // OK
}
def hello() {
"Hello World!".print();
}
##R
main <- function() {
hello() # OK
}
hello <- function() {
write("Hello World!", stdout())
}
main()
##Forth
: MAIN S" HELLO" EVALUATE ; \ OK
: HELLO ." Hello World!" CR ;
MAIN
##BASIC
10 GOSUB 30:REM OK
20 END
30 PRINT "Hello World!"
40 RETURN
##COBOL
000010 IDENTIFICATION DIVISION.
000020 PROGRAM-ID. MAIN.
000030 PROCEDURE DIVISION.
000040 CALL 'HELLO' OK
000050 STOP RUN.
000060 PROGRAM-ID. HELLO.
000070 PROCEDURE DIVISION.
000080 DISPLAY "HELLO WORLD!".
000090 END PROGRAM HELLO.
000100 END PROGRAM MAIN.
##NASM
.text
global _start
_start: call hello ; OK
mov eax, 60
xor edi, edi
syscall
.text
hello: mov eax, 1
mov edi, eax
lea rsi, [msg]
mov edx, len
syscall
ret
.rodata
msg: db 'Hello World!', 10
len equ $-msg
##Oz
functor
import
Application
System
define
proc {Main}
{Hello} % OK
end
proc {Hello}
{System.showInfo 'Hello World!'}
end
{Main}
{Application.exit 0}
end
##Simula
Begin
Procedure main;
Begin
hello ! OK ;
End;
Procedure hello;
Begin
OutText ("Hello World!");
Outimage;
End;
main;
End;
##Verilog
module main;
initial
begin
hello(); // OK
$finish;
end
task hello;
begin
$display("Hello World!");
end
endtask
endmodule
##ilasm
.assembly hello {}
.assembly extern mscorlib {}
.method static void main()
{
.entrypoint
call void hello() // OK
ret
}
.method static void hello()
{
ldstr "Hello World!"
call void [mscorlib]System.Console::WriteLine(string)
ret
}
##Julia
function main()
hello() # OK
end
function hello()
println("Hello World!")
end
main()
##Haxe
class HelloWorld {
static public function main():Void {
hello(); /* OK */
}
static public function hello():Void {
Sys.println("Hello World!");
}
}
##Yabasic
hello() // OK
End
Sub hello()
Print "Hello World!"
End Sub
##Falcon
hello() // OK
function hello()
> "Hello World!"
end
##PARI/GP
main()={hello();} \\ OK
hello()={print("Hello World!");}
main()
##Pawn
main()
{
hello() /* OK */
}
hello()
{
printf("Hello World!\n")
}
##LOLCODE
HAI 1.2
HOW IZ I MAIN
I IZ HELLO MKAY BTW OK
IF U SAY SO
HOW IZ I HELLO
VISIBLE "HAI WORLD!"
IF U SAY SO
I IZ MAIN MKAY
KTHXBYE
##ALGOL 68
PROC main = VOID: hello; # OK #
PROC hello = VOID: print (("Hello World!", new line));
main
##MASM
main macro
hello ; OK
endm
hello macro
echo Hello World!
endm
main
end
実行画面
C:\Users\fujita\Documents>type hello.asm
main macro
hello ; OK
endm
hello macro
echo Hello World!
endm
main
end
C:\Users\fujita\Documents>ml64 /c /Fonul hello.asm
Microsoft (R) Macro Assembler (x64) Version 14.16.27027.1
Copyright (C) Microsoft Corporation. All rights reserved.
Assembling: hello.asm
Hello World!
C:\Users\fujita\Documents>
#lang "qb"
GoSub hello:' OK
End
hello:
Print "Hello World!"
Return
##ABC
HOW TO MAIN:
HELLO \ OK
HOW TO HELLO:
WRITE "Hello World!"
MAIN
(def start!
(lambda (event)
(hello! 0))) ; OK
(def hello!
(lambda (event)
(print! "Hello World!")))
##B
main() {
hello(); /* OK */
return(0);
}
hello() {
puts("Hello World!");
}
main() {
hello(); // OK
}
hello() {
print("Hello World!");
}
main();
##Boo
def main():
hello() # OK
def hello():
print "Hello World!"
main()
おわりに
まだまだ気が向いたら追加する。