tags:

views:

107

answers:

5

You can't have code outside of functions except for declarations, definitions and preprocessor directives.

Is that statement accurate, or is there something I'm missing? I'm teaching my nephew to program, and he was trying to put a while loop before main. He's pretty young, I want to give him a hard simple rule that he can understand.

A: 

Yes- you can't stick random executable code outside functions.

DeadMG
+2  A: 

Not quite -- you can also put expressions in global variable declarations:

int myGlobalVar = 3 + SomeFunction(4) - anotherGlobalVar;

But you can only put expressions here, which have to evaluate to the value you're initializing the global with. You cannot put full statements (no blocks of code, no if statements, no loops, etc.). This code will get executed before main() gets a chance to run, so be careful with what you do here. I'd recommend against calling functions in global initializers unless you can't avoid it.

Adam Rosenfield
A: 

Yes, every kind of statement that does something must reside inside a context that can use it (this doesn't apply to variable initialization).

This because C++ is a structured programming language that encloses its behaviour inside procedures, as opposed to unstructured ones in which you have just one level of code and no scopes.

Jack
A: 
  • For your nephew:
    no, you can't do it.
  • For yourself:
    The compiler's input is technically what you get after the preprocessor is run. So, let's leave preprocessor out. After it has worked, you get a C++ program which is a sequence of declarations. Some delcarations may also be definitions, and some definitions (like function definitions) may have statements inside them.
    HTH
Armen Tsirunyan
The source code that we write contains preprocessor directives. So I don't see your point.
PigBen
@PigBen: Yeah, but preprocessor directives are mere text substitutions. It's PREprocessor. After the macros have expanded and conditionally some parts of code are left out, you get a C++ translation unit. That is what the compiler operates on. I mean that whether or not you can put a macro outside or inside a function is determined solely by what the macro expands to.
Armen Tsirunyan
@Armen -- But I didn't mention macros. I said preprocessor directives. And preprocessor directives, including macro definitions, can be put outside of functions.
PigBen
@PigBen: Outside... what? A function? What is a function? There is no notion of function when the preprocessor runs. Imagine this. And "x.h" file contains `void f(` and "y.h" contains `)`. Include x.h and y.h and add `{}` and you get a function...
Armen Tsirunyan
@Armen -- So, you're saying that we don't actually write functions then. We write preprocessor code that get's turned into functions. You should write a book, because all the ones I've ever read have gotten it wrong. Look, I don't want to get in to a big argument here. You're implying that I don't know what the preprocessor does, I get it. But do you really think that what you're saying is actually useful to an 11 year old? When we write code, we do in fact see things that we call functions, correct? And we see other things that we call preprocessor directives, correct?
PigBen
@PigBen: Oh, I am sorry that you think that I am trying to imply that you don't know something. That is not the case. 2 last questions: correct, correct. I don't want to get into an argument either. I just provided an answer which I believe is terminologically and conceptually more precise. I suppose you're right that it is may not be useful (although note that the part is marked as `for yourself` and not for the 11yo), but then, I don't want to repeat other answers. Sorry again
Armen Tsirunyan
@ToWhomeverItMayconcern: Please be so kind as to leave a comment explaining the downvote. So that I can learn to answer better (which often involves being less correct). Thank you.
Armen Tsirunyan
@Armen -- That was me. No offense, but you must understand why I wouldn't find this answer useful. Even though our little semi-heated exchange is over, it doesn't change the fact that I still don't find the answer useful. I will remove the downvote because I know you genuinely intended to be helpful though. Oops, it says my vote is locked unless your answer is edited. Change the colons to dashes, and I'll remove the downvote.
PigBen
A: 

Well, there's namespaces...and the stuff Adam Rosenfield mentioned...and there's also exception try/catch that can be sort of external to functions. Unfortunately, I can't remember the syntax and can't find it with google.

Noah Roberts