A friend of mine is getting into coding. He was asking me a bit about what language to learn and how they are different. He was curious about functions in particular.
To show him the differences I decided to write a very simple program to calculate the area of a triangle in 5 different languages. Each program is run at the command prompt. I tried to write the program in more or less the exact same way, somewhat ignoring a couple conventions in order to make each program as identical to the others as I could.
Python:
#triangle.py #run at command prompt with python triangle.py def triangle_area(base, height): area = (1.0/2) * base * height return area a1 = triangle_area(10, 2) print a1
Ruby:
#triangle.rb #run at command prompt with ruby triangle.rb def triangle_area(base, height) area = (1.0/2) * base * height return area end a1 = triangle_area(10, 2) print a1
For all the Ruby (and Rails) vs. Python (and Django) debates, these two languages look nearly identical in these examples. That doesn’t hold true forever, though. The main difference is that Python starts the function definition (the inside of the “black box”) with a colon. The function ends when the code is no longer indented – white space matter a lot in Python compared with other languages. Ruby on the other hand does not use the colon and ends the function instead with “end”.
JavaScript:
// triangle.js // run at command line with a program such as node – e.g., node triangle.js function triangleArea(base, height) { var area = (½) * base * height; return area; } var a1 = triangleArea (10, 2); console.log(a1);
JavaScript, in part due to its history and orientation to the web, does printing to the prompt a bit differently.
PHP:
<?php // triangle.php // run at command prompt with php triangle.php function triangle_area($base, $height) { $area = (½) * $base * $height; return $area; } $a1 = triangle_area(10, 2); print $a1; print “n”; ?>
Many people think PHP is ugly. I think it’s the dollar signs and question marks. Somehow it feels cheap and uncertain.
Java:
/** Triangle.java Must be compiled first Run at command prompt javac Triangle.java Then run java Triangle **/ class Triangle { public static double triangleArea(double base, double height) { /** Need 1.0 to get calculation to work right – indicates double **/ double area = ((1.0/2) * base * height); return area; } public static void main(String args[]) { double a1 = triangleArea(10.0, 2.0); System.out.println(a1 + “n”); } }
This is the only example that needs to be compiled. Complied languages generally run faster and programming in languages that need to be compiled is sometimes seen as “harder core”, though that’s a somewhat outdated view. Remember – right tool for the job!
I’ll do this again soon .. maybe adding R to the mix.
Perl:
sub triangle_area($base, $height) { return ($base*$height)/2; }
print &triangle_area(10, 2);
[…] from my friend Mike Zamansky that referenced a post by Alfred Thompson which itself referenced my last post to this blog. It was fun to see my post being discussed. Some commenting back and forth on both […]