Code Outside Functions

Code outside functions

You can't write code outside of functions. The only things you can have outside of functions are declarations such as global variable declarations (usually a bad idea), function declarations etc. Try putting it in a function like int main(){}

Why does a line of code that is outside a function (when the function is called in another file) prints out?

just to expand on my comment above, but imagine that your module a contains the following code:

def adult(x):
return x >= MIN_YEARS


MIN_YEARS = 18

This works perfectly fine, but only because the entire module is loaded when you call import a or from a import adult.

But if you didn't want a block of code to be run whenever a module was imported, you can hide that logic away within an if __name__=='__main__:' block at the end of the file, as @Tim Roberts also mentioned. So with the below approach, you won't see hello world printed whenever this module is imported by another one.

def adult(x):
return x >= 18


if __name__ == '__main__':
print('hello world')

Running C++ code outside of functions scope

One way of solving it is to have a class with a constructor that does things, then declare a dummy variable of that class. Like

struct Initializer
{
Initializer()
{
// Do pre-main initialization here
}
};

Initializer initializer;

You can of course have multiple such classes doing miscellaneous initialization. The order in each translation unit is specified to be top-down, but the order between translation units is not specified.

Is the C main function optional and can you write code outside of it?

Normal complete C programs hosted in an operating system must have a main routine.

Projects on GitHub may be mere portions of programs, such as a collection of routines intended to be used in other programs. (Such collections are often called libraries.) If source code is not intended to be a complete program by itself, then it does not need to have a main routine. A main routine will be added later, by somebody who uses the collection of routines in their own program.

C source code can also be compiled and used in combination with source code written in other programming languages. The behavior of this is not specified by the C standard, so it is specific to the various developer tools used when doing this. Such a hybrid program must have some main routine, but it may be called something other than main. Nonetheless, main has become very popular as the name of the main routine, so it is used very frequently.

C source code can be used for special software, such as operating system kernels. The C standard describes a freestanding environment, in contrast to a hosted environment. In a freestanding environment, many things are customized to the specific system, including how the starting address of the program is set. In this case, the main entry point might be called start instead of main, for example, and the address of that entry point might be conveyed to the hardware in some special data structure particular to the hardware.

Regarding code outside of functions, that may be initialization expressions. (There are strict limits on what expressions can be used in initialization outside of functions. You cannot write general C code in those expressions.) You would have to show specific examples to get answers about that.

code outside functions not remembered and running every time

To add to what @TheMaster said, Google Apps Script is stateless. Meaning each invocation has to process the whole project. So when your onEdit function runs, it processes everything. For example:

var global = "hi";

function something(){ ... }

function onEdit()
{
Logger.log(global);
global = "bye";
}

something();

Every time the onEdit function triggers, it will run through all of the code. In this example, every time onEdit triggers, it'll set global to hi, run something(), and then run onEdit().

I hope that make sense?

Is it possible to call a function outside of main()?

It's possible, but unnecessary.

Instead, wrap it in a class that closes it in the destructor, like you did with the other objects.

Destructors are called in the reverse order, which means that if you create the display first, it'll die last.


The way you would've called it after main is, similarily, from a destructor of a global or function-local static object. A function-local static is better than a global variable because it avoids the static init order fiasco.



Related Topics



Leave a reply



Submit