Tuesday, August 2, 2016

blocks

What's unrealistic in those examples is that a program is not composed of just function calls. I did initialize some variables on the page, but - if you're a programmer, you'll be wondering if I'm aware of this - at some point a program needs to include, well, I'm not entirely sure what to call them, but I'll call them actual statements.

Let me illustrate this.

function thefunction() {
var v = "v"
printit(v)

function printit(whattoprint) {
println whattoprint}

}

For non-programmers, we would call thefunction somewhere in our code, and then you can see a variable declaration, var v, and a value assigned to v, the string "v", that is, text which reads v, and then there's a function call, printit(v). So, printit(v) calls the printit function, and passes the variable v to printit. And so far it's all variable declarations and function calls, but the printit function consists of what I'm calling an actual statement, println whattoprint. (The contents of the variable v was placed in the variable whattoprint when printit was called, and maybe you can see from this example how that works.)

By the way, I looked up the println statement today, because I see it in programming examples all the time, but I couldn't get it to do anything in my own code, so I'm not sure what it does. I'm using it here to indicate that something gets displayed on the screen. So the statement println whattoprint directly tells the computer to do something. "Actual statements" directly tell the computer to do something.

Understand that variable declarations (var v) and assignments (v = "v") are statements (here, declaration and initialization via assignment are combined into a single statement), as are function calls, and function declarations. (function printit(whattoprint) is a function declaration.) Maybe variable statements could be called actual statements, actually, but function calls ... they just pass the action to another part of the program.

And we could write a perfectly correct program which consists only of function calls:

do_something().

The computer would accept that code without objection. It wouldn't do anything, because there are no actual statements, but it wouldn't reject the code.

Taking that a step further:

do_something()

function do_something() {
do_this()
do_that()}.

Again, it's perfectly valid. It doesn't do anything, but it does run.

So, is there any reason to write that kind of code? I mean, as a way to get the computer to do something, no, but as a way to describe the structure of a program, actually, there is.