Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...