Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

javascript - Difference between statement and function

It's almost midnight and I just got a question in my head is "for loop" a statement or a function.

I always thought it is a statement, but I did a google search on it being a function and there are indeed results for that. So what is it? And in that case what is the difference between function and statement?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

print "Hello World"

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

function(a) { return a * 2 }

A control structure, also known as a compound statement, is a statement that 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 worth noting is that an expression is a piece of code 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...