Saturday, May 9, 2009

pass the variable, please


One property that has tremendous bearing on any programming language is how variables are passed. There are generally two methods, passing by reference and passing by value.

Passing by reference takes the memory location of the variable or pointer and passes that into the function that takes the parameter. In short, this means that any work or changes done to the object or primitive is changed for good.

The other method is passing by value. This copies the reference to the object or variable and passes it in to the function. This allows for manipulation with the data from the variable or object without changing the actual contents of the data.

Both of these methods are valuable, and in my opinion the languages that can do both (like C#) are especially useful. In fact, this is one of the differences between Java and C#, Java only passes by value, C# allows passing by reference (through the ref keyword.) Without being able to pass by reference, Java becomes a more strongly-typed langauge, you have to go further to manipulate actual data. However, this also makes a simple program like swapping two string variables inordinately complicated compared to languages that pass by reference.

Knowing which technique the language you are using implements is of primary importance, failure to recognize whether a language passes by reference or by value will cause hours of frustration in seemingly inexplicable errors and unexpected return values.

Saturday, May 2, 2009

Scripting Languages



One enigmatic question that I find people often have a hard time giving a straight answer to is "What is the difference between a 'normal' programming language, such as C++, and a scripting language." This question leads to a lot of backpedalling and out spews a lot of general non-specific answers.

The term 'Scripting Language' refers to one of two different things. The specific definition refers to a programming language that controls software. An example of this would be QuakeC, which allows writing scripts for the popular fps game, Quake. Another example would be ASP, which works on the server side to dynamically generate web pages.

The generic term 'Scripting Language' refers to any language, whether used for specific applications or for general purposes, which is interpreted instead of compiled. An example of this general definition would be Python or Ruby.

"Aaah, now I get it." But what exactly is the difference between a language being compiled and a language being interpreted. An interpreted language is one that instead of compiled, is 'interpreted' and run at the same time. Essentially an interpreter is a dynamic compiler. it interprets each line of code in your program into machine code and then runs it at the moment. This contrasts to a language that must be compiled into machine code all at once and then run.

While an interpreted language runs slower, the syntax is often more powerful, interpreted languages allow for things such as implicit typing, which means you don't have to declare the types of your variables, and that you can change them on the fly. Programs or 'Scripts' often take a lot less time to write then if they were written in an a compiled language.

As a last note, I prefer the stricter definition of "Scripting Language", and generally so do programmers who use languages such as Python. If you have any expert knowledge of interpreted languages, please share below.