Newbie question about srand

This is a discussion on Newbie question about srand within the C++ Programming forums, part of the General Programming Boards category; I am new to C++ and still trying to understand the nuances of the the syntax. In the below example ...

Newbie question about srand

I am new to C++ and still trying to understand the nuances of the the syntax. In the below example i am using the srand() function to set my random seed. I am just wondering why srand needs to be in the main part of my program and cannot be outside in the front end of the program?

I am using global variables mostly because i am teaching myself and as a result I prob have bad habits(I am using the jumping into c++ online book). I was just reading global variables are not the way to go in most cases. I have used them for so long that i have become used to using them...

I am not sure i understood your 2nd response. Could you give an example?

Thank you again for your time, I still dont have a good sense on how to ask the proper questions and often times i lack the vocab needed to do a useful search.

Don't do that by the way, as it would get executed before the srand call and thus not end up random at all.

Edit: Actually your line 5 is already an example of that.

Thank you for the reply. That makes sense however I am not quite clear why I call a function to int a variable but cannot call a function straight up, as in the srand case. What is the logic behind syntax?

That makes sense however I am not quite clear why I call a function to int a variable but cannot call a function straight up, as in the srand case. What is the logic behind syntax?

Well, if you just examine it at one level (at file scope), a program is a sequence of declarations. A function prototype is a declaration of that function; a function definition is also a declaration of that function. However, a function call is not a declaration of that function, hence you cannot place it at file scope. But within the syntax of the initialisation of a variable, it is okay, since that syntax is syntax to define the variable, and a definition of a variable is a declaration of that variable.

Well, if you just examine it at one level (at file scope), a program is a sequence of declarations. A function prototype is a declaration of that function; a function definition is also a declaration of that function. However, a function call is not a declaration of that function, hence you cannot place it at file scope. But within the syntax of the initialisation of a variable, it is okay, since that syntax is syntax to define the variable, and a definition of a variable is a declaration of that variable.

Laser,

Thank you, it is comming together slowly. So if i understand, function calls are allowed in the but not at the file scope level becuase that portion of the program is for declarations only. Another example to clairify, I could call a function within a function definition whether i am in the Main function or not becuase i am declareing that function is this correct?
Thank you again,