Translations from Turing
From Compsci.ca Wiki
(Difference between revisions)
Line 283: | Line 283: | ||
sayHelloWorld();</pre> | sayHelloWorld();</pre> | ||
+ | |||
+ | == Functions and an argument == | ||
+ | |||
+ | Of course, we're still just saying "Hello, world!". There's no flexibility. Instead, let's create a procedure "say_hello" which takes one string argument "name" indicating who the procedure should greet. For this example, we'll greet "Bob". | ||
+ | |||
+ | This one will demonstrate specifying a procedure with an argument and then calling it that way. | ||
+ | |||
+ | === Turing === | ||
+ | |||
+ | <pre>procedure sayHello(name : string) | ||
+ | put "Hello, " + name + "!" | ||
+ | end sayHello | ||
+ | |||
+ | sayHello("Bob")</pre> | ||
+ | |||
+ | === C === | ||
+ | |||
+ | <pre>#include <stdio.h> | ||
+ | |||
+ | void say_hello(const char * name); | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | say_hello("Bob"); | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | void say_hello(const char * name) | ||
+ | { | ||
+ | printf("Hello, %s!\n", name); | ||
+ | }</pre> | ||
+ | |||
+ | === C++ === | ||
+ | |||
+ | <pre>#include <iostream> | ||
+ | #include <string> | ||
+ | |||
+ | void say_hello(std::string name); | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | say_hello("Bob"); | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | void say_hello(std::string name) | ||
+ | { | ||
+ | std::cout << "Hello, " << name, "!" << std::endl; | ||
+ | }</pre> | ||
+ | |||
+ | === Java === | ||
+ | |||
+ | <pre>import java.lang.*; | ||
+ | import java.io.*; | ||
+ | |||
+ | public class HelloWorld { | ||
+ | public static void main(String[] args) { | ||
+ | sayHello("Bob"); | ||
+ | } | ||
+ | |||
+ | private static void sayHello(String name) { | ||
+ | System.out.println("Hello, " + name + "!"); | ||
+ | } | ||
+ | }</pre> | ||
+ | |||
+ | === C# === | ||
+ | |||
+ | <pre>using System; | ||
+ | |||
+ | public class HelloWorld | ||
+ | { | ||
+ | public static void Main(string[] args) | ||
+ | { | ||
+ | SayHello("Bob"); | ||
+ | } | ||
+ | |||
+ | private static void SayHello(string name) | ||
+ | { | ||
+ | Console.WriteLine("Hello, " + name + "!"); | ||
+ | } | ||
+ | }</pre> | ||
+ | |||
+ | === D === | ||
+ | |||
+ | <pre>import std.stream; | ||
+ | |||
+ | void main() | ||
+ | { | ||
+ | sayHello("Bob"); | ||
+ | } | ||
+ | |||
+ | void sayHello(char[] name) | ||
+ | { | ||
+ | stdout.writefln("Hello, %s!", name); | ||
+ | }</pre> | ||
+ | |||
+ | === Perl === | ||
+ | |||
+ | <pre>say_hello("Bob"); | ||
+ | |||
+ | sub say_hello { | ||
+ | my $name = shift; | ||
+ | print "Hello, $name!\n"; | ||
+ | }</pre> | ||
+ | |||
+ | === Python === | ||
+ | |||
+ | <pre>def say_hello(name): | ||
+ | print "Hello, %(name)s!" % {"name": name} | ||
+ | |||
+ | say_hello("Bob")</pre> | ||
+ | |||
+ | === Ruby === | ||
+ | |||
+ | <pre>def say_hello(name) | ||
+ | puts "Hello, #{name}!" | ||
+ | end | ||
+ | |||
+ | say_hello("Bob")</pre> | ||
+ | |||
+ | === O'Caml === | ||
+ | |||
+ | <pre>let say_hello name = | ||
+ | print_endline ("Hello, " ^ name ^ "!") | ||
+ | |||
+ | say_hello "Bob"</pre> | ||
+ | |||
+ | === Eiffel === | ||
+ | |||
+ | <pre>class HELLO_WORLD | ||
+ | creation {ANY} make | ||
+ | feature {ANY} | ||
+ | make is | ||
+ | do | ||
+ | say_hello("Bob") | ||
+ | end | ||
+ | feature {NONE} | ||
+ | say_hello(name : STRING) is | ||
+ | do | ||
+ | std_output.put_string("Hello, " + name + "!") | ||
+ | std_output.put_new_line | ||
+ | end | ||
+ | end</pre> | ||
+ | |||
+ | === Pascal === | ||
+ | |||
+ | This one should work, but FreePascal is giving me a syntax error | ||
+ | |||
+ | <pre>program Strings; | ||
+ | var | ||
+ | procedure SayHello(Name : String); | ||
+ | begin | ||
+ | WriteLn(Name) | ||
+ | end; | ||
+ | begin | ||
+ | SayHello('Bob') | ||
+ | end.</pre> | ||
+ | |||
+ | === Ada === | ||
+ | |||
+ | Strings in Ada are tricky enough that I've left this one out | ||
+ | |||
+ | === Javascript === | ||
+ | |||
+ | <pre>function sayHello(name) { | ||
+ | document.write("Hello, " + name + "!"); | ||
+ | } | ||
+ | |||
+ | sayHello("Bob");</pre> | ||
== Credits == | == Credits == | ||
Author: [[Wtd]] | Author: [[Wtd]] |
Latest revision as of 06:57, 12 October 2008
Contents |
Hello, world!
Turing
% This is a comment put "Hello, world!"
C
/* This a comment */ #include <stdio.h> int main() { puts("Hello, world!"); return 0; }
C++
// This is a comment #include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; }
Java
// This is a comment import java.lang.*; import java.io.*; public static void HelloWorld { public static void main(String[] args) { System.out.println("Hello, world!"); } }
C#
// This is a comment using System; public class HelloWorld { public static void Main(string[] args) { Console.WriteLine("Hello, world!"); } }
D
// This is a comment import std.stream; void main() { stdout.writeLine("Hello, world!"); }
Perl
# This a comment print "Hello, world!\n";
Python
# This a comment print "Hello, world!"
Ruby
# This a comment puts "Hello, world!"
O'Caml
(* This a comment *) print_endline "Hello, world!"
Eiffel
-- This is a comment class HELLO_WORLD creation {ANY} make feature {ANY} make is do std_output.put_string("Hello, world!") std_output.put_new_line end end
Pascal
(* This a comment *) program HelloWorld; begin WriteLn('Hello, world!') end.
Ada95
-- This is a comment with Ada.Text_IO; use Ada.Text_IO; procedure Hello_World is begin Put_Line("Hello, world!"); end Hello_World;
Javascript
// This is a comment document.write("Hello, world!<br/>");
A procedure
Now, abstracting the "Hello, world!" into a procedure called "say_hello_world".
Note: That name may appear slightly different depending on the naming conventions of a language. Where possible, forward declarations are used.
Turing
procedure sayHelloWorld put "Hello, world!" end sayHelloWorld sayHelloWorld
C
#include <stdio.h> void say_hello_world(); int main() { say_hello_world(); return 0; } void say_hello_world() { puts("Hello, world!"); }
C++
#include <iostream> void say_hello_world(); int main() { say_hello_world(); return 0; } void say_hello_world() { std::cout << "Hello, world!" << std::endl; }
Java
import java.lang.*; import java.io.*; public class HelloWorld { public static void main(String[] args) { sayHelloWorld(); } private static void sayHelloWorld() { System.out.println("Hello, world!"); } }
C#
using System; public class HelloWorld { public static void Main(string[] args) { SayHelloWorld(); } private static void SayHelloWorld() { Console.WriteLine("Hello, world!"); } }
D
import std.stream; void main() { sayHelloWorld(); } void sayHelloWorld() { stdout.writeLine("Hello, world!"); }
Perl
say_hello_world; sub say_hello_world { print "Hello, world!"; }
Python
def say_hello_world(): print "Hello, world!" say_hello_world()
Ruby
def say_hello_world puts "Hello, world!" end say_hello_world
O'Caml
let say_hello_world () = print_endline "Hello, world!" say_hello_world ()
Eiffel
class HELLO_WORLD creation {ANY} make feature {ANY} make is do say_hello_world end feature {NONE} say_hello_world is do std_output.put_string("Hello, world!") std_output.put_new_line end end
Pascal
program HelloWorld; var procedure SayHelloWorld; begin WriteLn('Hello, world!') end; begin SayHelloWorld end.
Ada
with Ada.Text_IO; use Ada.Text_IO; procedure Hello_World is procedure Say_Hello_World is begin Put_Line("Hello, world!"); end Say_Hello_World; begin Say_Hello_World; end Hello_World;
Javascript
function sayHelloWorld() { document.write("Hello, world!"); } sayHelloWorld();
Functions and an argument
Of course, we're still just saying "Hello, world!". There's no flexibility. Instead, let's create a procedure "say_hello" which takes one string argument "name" indicating who the procedure should greet. For this example, we'll greet "Bob".
This one will demonstrate specifying a procedure with an argument and then calling it that way.
Turing
procedure sayHello(name : string) put "Hello, " + name + "!" end sayHello sayHello("Bob")
C
#include <stdio.h> void say_hello(const char * name); int main() { say_hello("Bob"); return 0; } void say_hello(const char * name) { printf("Hello, %s!\n", name); }
C++
#include <iostream> #include <string> void say_hello(std::string name); int main() { say_hello("Bob"); return 0; } void say_hello(std::string name) { std::cout << "Hello, " << name, "!" << std::endl; }
Java
import java.lang.*; import java.io.*; public class HelloWorld { public static void main(String[] args) { sayHello("Bob"); } private static void sayHello(String name) { System.out.println("Hello, " + name + "!"); } }
C#
using System; public class HelloWorld { public static void Main(string[] args) { SayHello("Bob"); } private static void SayHello(string name) { Console.WriteLine("Hello, " + name + "!"); } }
D
import std.stream; void main() { sayHello("Bob"); } void sayHello(char[] name) { stdout.writefln("Hello, %s!", name); }
Perl
say_hello("Bob"); sub say_hello { my $name = shift; print "Hello, $name!\n"; }
Python
def say_hello(name): print "Hello, %(name)s!" % {"name": name} say_hello("Bob")
Ruby
def say_hello(name) puts "Hello, #{name}!" end say_hello("Bob")
O'Caml
let say_hello name = print_endline ("Hello, " ^ name ^ "!") say_hello "Bob"
Eiffel
class HELLO_WORLD creation {ANY} make feature {ANY} make is do say_hello("Bob") end feature {NONE} say_hello(name : STRING) is do std_output.put_string("Hello, " + name + "!") std_output.put_new_line end end
Pascal
This one should work, but FreePascal is giving me a syntax error
program Strings; var procedure SayHello(Name : String); begin WriteLn(Name) end; begin SayHello('Bob') end.
Ada
Strings in Ada are tricky enough that I've left this one out
Javascript
function sayHello(name) { document.write("Hello, " + name + "!"); } sayHello("Bob");
Credits
Author: Wtd