If there is no assignment statement,how can this be done?How to change the balance variable?
You can't change variables without some sort of assignment operator.
I ask so because I know there are some so-called pure functional languages out there and according to the Turing complete theory,this must can be done too.
Not quite. If a language is Turing complete that means that it can calculate anything that the any other Turing complete language can calculate. It doesn't mean that it has to have every feature other languages have.
It's not a contradiction that a Turing complete programming language has no way of changing the value of a variable, as long as for every program that has mutable variables, you can write an equivalent program that does not have mutable variables (where "equivalent" means that it calculates the same thing). And in fact every program can be written that way.
Regarding your example: In a purely functional language you simply wouldn't be able to write a function that returns a different account balance each time it's called. But you'd still be able to rewrite every program, that uses such a function, in a different way.
Since you asked for an example, let's consider an imperative program that uses your make-withdraw function (in pseudo-code). This program allows the user to withdraw from an account, deposit to it or query the amount of money in the account:
account = make-withdraw(0)
ask for input until the user enters "quit"
if the user entered "withdraw $x"
account(x)
if the user entered "deposit $x"
account(-x)
if the user entered "query"
print("The balance of the account is " + account(0))
Here's a way to write the same program without using mutable-variables (I won't bother with referentially transparent IO because the question wasn't about that):
function IO_loop(balance):
ask for input
if the user entered "withdraw $x"
IO_loop(balance - x)
if the user entered "deposit $x"
IO_loop(balance + x)
if the user entered "query"
print("The balance of the account is " + balance)
IO_loop(balance)
if the user entered "quit"
do nothing
IO_loop(0)
The same function could also be written without using recursion by using a fold over the user input (which would be more idiomatic than explicit recursion), but I don't know whether you're familiar with folds yet, so I wrote it in a way that doesn't use anything you don't know yet.
Best Answer
How many lines of code are in the following program?
You probably answered 7 (or 6 if you didn't count the blank line, or 4 if you didn't count the braces).
Your compiler, however, sees something very different:
Yes, that's 18.7 KLOC just for a "Hello, world!" program. The C++ compiler has to parse all that. This is a major reason why C++ compilation takes so long compared to other languages, and why modern languages eschew header files.
A better question would be
Why does C++ have header files?
C++ was designed to be a superset of C, so it had to keep header files for backwards compatibility.
OK, so why does C have header files?
Because of its primitive separate compilation model. The object files generated by C compilers don't include any type information, so in order to prevent type errors you need to include this information in your source code.
Adding the proper type declarations fixes the bug:
Notice that there are no
#include
s. But when you use a large number of external functions (which most programs will), manually declaring them gets tedious and error-prone. It's much easier to use header files.How are modern languages able to avoid header files?
By using a different object file format that includes type information. For example, the Java *.class file format includes "descriptors" that specify the types of fields and method parameters.
This was not a new invention. Earlier (1987), when Borland added separately-compiled "units" to Turbo Pascal 4.0, it chose to use a new
*.TPU
format rather than Turbo C's*.OBJ
in order to remove the need for header files.