[ACCEPTED]-Difference between statement and function-ruby

Accepted answer
Score: 19

A for loop is a not usually a function, it 10 is a special kind of statement called a 9 flow control structure.

A statement is a command. It 8 does something. In most languages, statements 7 do not return values. Example:

print "Hello World"

A function is a subroutine 6 that can be called elsewhere in the program. Functions 5 often (but not necessarily) return values. Example:

function(a) { return a * 2 }

A 4 control structure, also known as a compound statement, is a statement that 3 is used to direct the flow of execution. Examples:

if (condition) then { branch_1 } else { branch_2 }
for (i = 0; i < 10; i += 1) { ... }

Also 2 worth noting is that an expression is a piece of code 1 that evaluates to a value. Example:

2 + 2

All examples are in pseudocode, not tied to any particular language. Also note that these are not exclusive categories, they can overlap.

Score: 8

Out of the three language tags you've chosen, I'm 88 only very familliar with Python, but I believe 87 many other languages have a similar view 86 of these concepts. All the example code 85 here is Python.

A statement is a thing that is executed; an 84 "instruction to do something" that the language 83 implementation understands. e.g.

print "Hello World"


def foo(n):
    return n + 1

if condition:
    print 'yay'
    print 'doh'

The above 82 block contains a print statement, a pass 81 statement, a function definition statement, and 80 an if/else statement. Note that the function 79 definition and the if/else statement are 78 compound statements; they contain other 77 statements (possibly many of them, and possibly 76 other compound statements).

An expression is something 75 that can be evaluated to produce a value. e.g.



2 * 6



The 74 above contains a numeric literal expression, a 73 string literal expression, an expression 72 involving numeric operators, a function 71 call expression, and the literal None expression. Other 70 than literals and variables, expressions 69 are made up of other expressions. In function(argument), function and 68 argument are also both expressions.

The key difference 67 is that statements are instructions that 66 tell the language implementation to "go 65 do something". Expressions are evaluated 64 to a value (which possibly requires to language 63 implementation to "go do something" on the 62 way).

A consequence of this is that anywhere 61 you see a value (including an expression), you 60 could substitute any other expression and 59 you would still get something that makes 58 some sort of sense. It may fail to compile, or 57 throw exceptions at runtime, or whatever, but 56 on at least some level you can understand 55 what's going on.

A statement can never appear 54 inside an expression (I believe this is 53 not true in Ruby and Javascript in some 52 sense, as they allow literal code blocks 51 and functions which are then used as a value 50 as a whole, and functions and code blocks 49 contain statements; but that's kind of different 48 from what I'm talking about). An expression 47 must have a value (even if it's an uninteresting 46 one like None). A statement is a command; it 45 doesn't make sense for it to appear as part 44 of an expression, because it has no value.

Many 43 languages also allow expressions to be used 42 as statements. The usual meaning of this 41 is "evaluate this expression to get a value, then 40 throw it away". In Python, functions that 39 always return None are usually used this way:


It's 38 used as a "command", so it looks like a 37 statement, but technically it's an expression, we 36 just don't use the value it evaluates to 35 for anything. You can argue that a "bare 34 expression" is one of the possible statements 33 in a language (and they're often parsed 32 that way).

Some languages though distinguish 31 between functions that must be used like 30 statements with no return value (often called 29 procedures) and functions that are used 28 like an expression, and give you errors 27 or warnings for using a function like a 26 statement, and definitely give you an error 25 for using a procedure as an expression.

So, if 24 foo is an expression, I can write 1 + foo and while 23 it may be result in a type error, it at 22 least makes that much sense. If foo is a statement, then 21 1 + foo is usually a parse error; the language 20 implementation won't even be able to understand 19 what you're trying to say.

A function on the other 18 hand, is a thing you can call. It's not 17 really either an expression or a statement 16 in itself. In Python, you use a def statement 15 to create a function, and a function call 14 is an expression. The name bound to the 13 function after you create it is also an 12 expression. But the function itself is a 11 value, which isn't exactly an expression when 10 you get technical, but certainly isn't a 9 statement.

So, for loops. This is a for loop 8 in Python:

for thing in collection:

Looks like a statement (a compound 7 statement, like an if statement). And to 6 prove it, this is utterly meaningless (and 5 a parse error):

1 + for thing in collection:

In some languages though, the 4 equivalent of a for loop is an expression, and 3 has a value, to which you can attempt to 2 add 1. In some it's even a function, not 1 special syntax baked into the language.

Score: 1

This answer is relevant to Python 2.7.2. Taken from 10 the python tutorial:

"4. More Control Flow Tools

4.2. for Statements: The for statement in Python 9 differs a bit from what you may be used 8 to in C or Pascal. Rather than always iterating 7 over an arithmetic progression of numbers 6 (like in Pascal), or giving the user the 5 ability to define both the iteration step 4 and halting condition (as C), Python’s for 3 statement iterates over the items of any 2 sequence (a list or a string), in the order 1 that they appear in the sequence."

More Related questions