10
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

いろいろな言語でHello World

Last updated at Posted at 2019-09-12

はじめに

C99
#include <stdio.h>

int hello(void); /* 関数プロトタイプのこの行を削除するとコンパイル時に警告 */

int main(void)
{
    hello();
    return 0;
}

int hello(void)
{
    puts("Hello World!");
    return 0;
}

Wandbox で実行

Pascal
program hello;

procedure hello; forward; (* 前方宣言のこの行を削除するとコンパイルエラー *)

procedure main;
begin
  hello
end;

procedure hello;
begin
  writeln('Hello World!')
end;

begin
  main
end.

Wandbox で実行

のように、前方で定義された関数や手続きを安全に呼び出すにはそれなりの記述を必要とする言語があり、そうでないものも存在する。Qiita の某記事のコメント欄で前方参照宣言等なしに前方で定義した関数等を呼び出せる(ように見える)言語等をいくつか挙げてみたらそこそこの数になってきたのでいい加減記事主にも迷惑だろうと引っ越しして記事化。
なお、

C99
#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

Wandbox で実行

のようなトンチ回答は対象としない。
実際よくわからんところで書いてるので内容は多くの人の参考になるものではあるまい。

##AWK

BEGIN {
    hello() # OK
}

function hello() {
    print "Hello World!"
}

ideone で実行

##Perl

&hello(); # OK

sub hello() {
    print "Hello World!"
}

ideone で実行

##Ruby

def main()
  hello() # OK
end

def hello()
  puts 'Hello World!'
end

main()

ideone で実行

##Python

def main():
    hello() # OK

def hello():
    print('Hello World!')

main()

Wandbox で実行

##PHP

<?php
  hello(); // OK

  function hello()
  {
    echo "Hello World!\n";
  }
?>

ideone で実行

##Dart

void main() {
    hello(); // OK
}

void hello() {
    print('Hello World!');
}

ideone で実行

##TypeScript

hello(); // OK

function hello() {
    console.log("Hello World!");
}

Wandbox で実行

##CoffeeScript

main = ->
  hello() # OK
  return

hello = ->
  console.log "Hello World!"
  return

main()

Wandbox で実行

##Groovy

class Ideone {
    static void main(String[] args) {
        Hello.hello() // OK
    }
}

class Hello {
    static void hello() {
    	println "Hello World!"
    }
}

ideone で実行

##Emacs Lisp

(defun main ()
  (hello)) ; OK

(defun hello ()
  (message "Hello World!"))

(main)

repl.it で実行

##Vim script

function Main()
    call Hello() " OK
endfunction
 
function Hello()
    echo "Hello World!"
endfunction

call Main()

Wandbox で実行

##Lua

function main()
    hello() -- OK
end

function hello()
    print "Hello World!"
end

main()

Wandbox で実行

##Tcl

proc main {} {
    hello ;# OK
}

proc hello {} {
    puts "Hello World!"
}

main

ideone で実行

##CMake

function(main)
  hello() # OK
endfunction()

function(hello)
  message("Hello World!")
endfunction()

main()

Wandbox で実行

##Make

main: hello # OK

hello:
        @echo "Hello World!"

※ 行頭の空白はタブ

##bc

define main() {
    dummy = hello() # OK
}

define hello() {
    print "Hello World!\n"
}

dummy = main()

ideone で実行

##dc

[lh x] sm # OK
[[Hello World!]p] sh
lm x

##Bash

#!/bin/bash
main() {
    hello # OK
}

hello() {
    echo "Hello World!"
}

main

ideone で実行

##Octave

function main
    hello # OK
end

function hello
    disp "Hello World!"
end

main

ideone で実行

##Scheme(Gauche)

(define (main args)
    (hello) ; OK
    0)

(define (hello)
    (display "Hello World!")
    (newline))

paiza.io で実行

##Tera Term macro

	call hello ; OK
	end

:hello
	dispstr "Hello World!"#13#10
	return

##Batch file(cmd.exe)

@echo off
	call :hello & :: OK
	exit/b

:hello
	echo Hello World!
	exit/b

##PowerShell

Function main() {
    hello # OK
}

Function hello() {
    Write-Output "Hello World!"
}

main

##JScript(WSH)

hello() // OK

function hello() {
    WScript.Echo("Hello World!")
}

##VBScript(WSH)

hello ' OK

Function hello()
    WScript.Echo "Hello World!"
End Function

##cpp

#define main hello // OK
#define hello Hello World!
main

Wandbox で実行

##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

TeXclip で実行

##Rexx

call hello /* OK */
exit
hello: procedure
    say 'Hello World!'
    return

codingground で実行

##D

import std.stdio;

void main()
{
    hello(); // OK
}

void hello()
{
    writeln("Hello World!");
}

ideone で実行

##Go

package main
import "fmt"

func main() {
    hello(); // OK
}

func hello() {
    fmt.Printf("Hello World!\n")
}

ideone で実行

##Rust

fn main() {
    hello(); // OK
}
 
fn hello() {
    println!("Hello World!");
}

ideone で実行

##Swift

func main() {
    hello() // OK
}

func hello() {
    print("Hello World!")
}

main()

Wandbox で実行

##C++

#include <iostream>

class hoge {
  public:
    hoge()
    {
        hello(); // OK
    }

    void hello()
    {
        std::cout << "Hello World!" << std::endl;
    }
};

int main()
{
    hoge hoge;
}

Wandbox で実行

##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!");
    }
}

ideone で実行

##Kotlin

fun main(args: Array<String>) {
    hello() // OK
}

fun hello() {
    println("Hello World!")
}

ideone で実行

##C#

public class Hogera
{
    public static void Main()
    {
    	Hello.hello(); // OK
    }
}

public class Hello
{
    public static void hello()
    {
        System.Console.WriteLine("Hello World!");
    }
}

ideone で実行

##Visual Basic .NET

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

ideone で実行

##Erlang

-module(prog).
-export([main/0]).
 
main() ->
    hello(). % OK
 
hello() ->
    io:format("Hello World!~n").

ideone で実行

##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

ideone で実行

##Scheme(Racket)

(define (main)
    (hello)) ; OK

(define (hello)
    (display "Hello World!")
    (newline))

(main)

ideone で実行

##Common Lisp

(defun main ()
    (hello)) ; OK

(defun hello ()
    (format t "Hello World!~%"))

(main)

ideone で実行

##Clojure

(defn -main []
  (eval (read-string "(hello)"))) ; OK

(defn hello []
  (println "Hello World!"))

(-main)

##Haskell

main = hello -- OK
hello = putStrLn "Hello World!"

ideone で実行

##Scala

object Main {
  def main(args: Array[String]) {
    Hello.hello() // OK
  }
}

object Hello {
  def hello() {
    printf("Hello World!\n")
  }
}

ideone で実行

##Nemerle

class Main {
	static Main() : void {
		Hello.hello(); // OK
	}
}

class Hello {
	public static hello() : void {
		 System.Console.WriteLine("Hello World!");
	}
}

ideone で実行

##Nice

void main(String[] args)
{
    hello(); // OK
}
 
void hello()
{
    System.out.println("Hello World!");
}

ideone で実行

##Fantom

class FantomSay {
    Void main(Str[] args) {
        Hello.hello() // OK
    }
}
 
class Hello {
    static Void hello() {
        echo("Hello World!")
    }
}

ideone で実行

##Icon

procedure main()
    hello() # OK
end
 
procedure hello()
    write("Hello World!")
end

ideone で実行

##Pike

int main() {
    hello(); // OK
    return 0;
}
 
void hello() {
    write("Hello World!\n");
}

ideone で実行

##Gosu

hello() // OK
 
private function hello() : void {
    print("Hello World!")
}

ideone で実行

##Prolog

:- initialization(main).
main :- hello, % OK
        halt.
hello :- write('Hello World!'), nl.

ideone で実行

##Crystal

hello # OK

def hello()
  puts "Hello World!"
end

Wandbox で実行

##Pony

actor Main
  new create(env: Env) =>
    Hello.hello(env) /// OK

actor Hello
  be hello(env: Env) =>
    env.out.print("Hello World!")

Wandbox で実行

##Rill

import std.stdio;

def main() {
    hello(); // OK
}

def hello() {
    "Hello World!".print();
}

Wandbox で実行

##R

main <- function() {
  hello() # OK
}
 
hello <- function() {
  write("Hello World!", stdout())
}
 
main()

ideone で実行

##Forth

: MAIN S" HELLO" EVALUATE ; \ OK
: HELLO ." Hello World!" CR ;
MAIN

ideone で実行

##BASIC

10 GOSUB 30:REM OK
20 END
30 PRINT "Hello World!"
40 RETURN

IchigoJam web で実行(RUN[enter])

##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.

ideone で実行

##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

ideone で実行

##Oz

functor
import
   Application
   System
define
   proc {Main}
      {Hello} % OK
   end
   proc {Hello}
      {System.showInfo 'Hello World!'}
   end
   {Main}
   {Application.exit 0}
end

codingground で実行

##Simula

Begin
   Procedure main;
      Begin
         hello ! OK ;
      End;
   Procedure hello;
      Begin
         OutText ("Hello World!");
         Outimage;
      End;
   main;
End;

codingground で実行

##Verilog

module main;
  initial 
    begin
      hello(); // OK
      $finish;
    end
  task hello;
    begin
      $display("Hello World!");
    end
  endtask
endmodule

codingground で実行

##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
}

codingground で実行

##Julia

function main()
  hello() # OK
end
function hello()
  println("Hello World!")
end
main()

codingground で実行

##Haxe

class HelloWorld {
  static public function main():Void {
    hello(); /* OK */
  }
  static public function hello():Void {
    Sys.println("Hello World!");
  }
}

codingground で実行

##Yabasic

hello() // OK
End

Sub hello()
  Print "Hello World!"
End Sub

codingground で実行

##Falcon

hello() // OK
function hello()
    > "Hello World!"
end

codingground で実行

##PARI/GP

main()={hello();} \\ OK
hello()={print("Hello World!");}
main()

codingground で実行

##Pawn

main()
{
   hello() /* OK */
}

hello()
{
   printf("Hello World!\n")
}

codingground で実行

##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

codingground で実行

##ALGOL 68

PROC main = VOID: hello; # OK #
PROC hello = VOID: print (("Hello World!", new line));
main

codingground で実行

##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>

##FreeBASIC

#lang "qb"
GoSub hello:' OK
End
hello:
Print "Hello World!"
Return

codingground で実行

##ABC

HOW TO MAIN:
    HELLO \ OK

HOW TO HELLO:
    WRITE "Hello World!"

MAIN

Try It Online で実行

##Appleseed

(def start!
  (lambda (event)
    (hello! 0))) ; OK

(def hello!
  (lambda (event)
    (print! "Hello World!")))

Try It Online で実行

##B

main() {
    hello(); /* OK */
    return(0);
}

hello() {
    puts("Hello World!");
}

Try It Online で実行

##BeanShell

main() {
    hello(); // OK
}

hello() {
    print("Hello World!");
}

main();

Try It Online で実行

##Boo

def main():
    hello() # OK

def hello():
    print "Hello World!"

main()

Try It Online で実行

おわりに

まだまだ気が向いたら追加する。

10
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?