What is the difference between an interpreted language and a compiled language?

A compiled language (C, FORTRAN,...) is converted to a directly machine executable form once before it is executed many times. An interpreted language (BASIC, Python,...) is converted at every execution. In some cases source files are converted once to an intermediate form (bytecode, etc) and saved before execution so that subsequent runs can be performed more efficiently. But this intermediate form is still not directly executable like a compiled program.

For the record, you could have just typed that same question into Google.

Some languages are "both." Python, for example, can be executed as either a compiled program or as an interpreted language in interactive mode.

As far as I know, Python compiles scripts into a folder with a name like pycache. The sorta-compiled code is actually bytecode which is easier for the runtime to process, but the bytecode is still interpreted. When you install python you are given the option to pre-compile the standard packages.

commented: PHP also caches scripts in memory +34

Lost my longer reply as I'm on a new keyboard but on most machines today, assembler code is again expanded to microcode so it's like byte code. As you recall, long ago I was on a team to design and (edited) replace a soon to be discontinued 8 bit processor. We opted to use a microcode system to speed up the design process. No code ever ran without a little microcode interpreter work. This is how most CPUs work today.

But hey, this is like a rabbit hole, isn't it?

OK. How about this. A compiled program is one you use without having access to the source code. It's also a program that does not have to be repeatedly parsed during execution.

commented: Bytecode, machine code, etc. is slicing it thinly as it's is possible to have a CPU that understands bytecode. Look at Intel microcode updates. +17

I’m still waiting for someone to propose an answer in layman’s terms.

How about this...

If I am given a book in English and I then translate it into French and write it down, I have compiled it, as the translation only has to be done once.

If I am working as a translator at the UN and listen to an English speaker, then speak the same text in French then I am interpreting. If the English speaker says the same thing again, I must translate and speak it in French again.

The first case is compilation, the second is interpretation.

This can be a rabbit hole since today's CPUs can have microcode updates to execute the bytecode directly.

This could be called translation or did the CPU learn the new language?

RE bytecode/machine code/micro-code...

I used to program micro computers at the Health Sciences Centre research labs. This we back in the days of paper tape and 8" floppies. In those days interactive debugging consisted of coding a stop statement and toggling in hand assembled code from the front panel switches.

imsai_8080.jpg

I consider the hex values I toggled in to be the actual machine code. Computer scientists don't consider anything lower than that (micro-code) as the "real" machine language. So once code has been converted (compiled) into these hex values they are in machine code form.

In the old days there were actually two steps to get an exe file.

  1. compile program.c to produce program.o (object module)
  2. link edit one or more object modules to produce program.exe

Easier to do on today's systems. Much more complicated on IBM mainframes (I hated JCL). For the record, even until 1998 I was still using front panel switches on our AGC/SCADA mainframes on a semi-regular basis.

Let me tip my hand here about my old work with FPGAs. We built our replacement processor with such and one model (Xilinx) let us replace the microcode at boot time. So it would be able to directly execute bytecode from say, Java.

Mind you this is rather old news so here's an example: https://www.electronicdesign.com/technologies/embedded-revolution/article/21764771/java-core-permits-direct-java-bytecode-execution

So Jim, are we living in a computer simulation?

If this is a computer simulation it must be the beta version with all the bugs still in.

The first case is compilation, the second is interpretation.

That suggests that every time I run a compiled program with the same input, I get the same output every time, and every time I run a script with the same input, I get a slightly different result each time. That isn’t necessarily true, is it? ;)

Interpreter

  • Translates program one statement at a time.

  • Interpreters usually take less amount of time to analyze the source code. However, the overall execution time is comparatively slower than compilers.

  • No Object Code is generated, hence are memory efficient.

  • Programming languages like JavaScript, Python, Ruby use interpreters.

Compiler

  • Scans the entire program and translates it as a whole into machine code.

  • Compilers usually take a large amount of time to analyze the source code. However, the overall execution time is comparatively faster than interpreters.

  • Generates Object Code which further requires linking, hence requires more memory.

  • Programming languages like C, C++, Java use compilers.

an interpreted language is one where the source code is executed directly by an interpreter at runtime, while a compiled language is one where the source code is translated into machine code before runtime. Both have their advantages and disadvantages, and the choice between them depends on the specific needs of the program.

For Jim, my view is that the answer is fuzzy since "it depends" on what the host system is. My last example was the CPU that took in Java bytecode.

For Dani, long ago I wrote a lot of code that would simulate an electronic design. We were looking at what variance the design had so the code used the Monte Carlo method. Two runs could give you two different answers. To achieve the same result each time you could seed the random number generator with the same seed but that wasn't the goal of said simulation. But as you noted: "same input." As time passed we changed from our own RNG to some API which changed the results slightly.

every time I run a script with the same input, I get a slightly different result each time

That would depend on several factors.

Are you absolutely certain all of the included packages are unchanged between runs? Whether compiled or interpreted, things external to your source can affect the output. Dot net libraries can be updated between runs. If your code uses random numbers then this can obviously affect the output. Obviously I can't say for certain without seeing the code. Again, this is an argument that is irrelevant to the compiled/interpreted question.

Programming languages like C, C++, Java use compilers.

I would consider Java to be an interpreted (or at best a hybrid) language. Java is converted to byte-code which is then run (interpreted) by the Java run-time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.