views:

72

answers:

3

Hi, first, sorry for the title but I really don´t know ho to summarize what I want to do. I am trying to write very simple "graphic" console game, just to learn basics of C++ and programming generally. When I have a function, I can pass value, or variable into that function while calling it. But I would like to do the same thing to the piece of code, but without using function. Becouse when function is called, program must actually jump to function, than return. So I thought, it would be more CPU-saving to just have that function built-in main, and just somehow select what that code should process. This could be done by passing value I want to process to some extra variable and let that "function" process that variable, but since I work with 2 dimensional fields, I need to use 2 for cycles to actually copy user-selected field to my work field. So what I want to know is, is there some way to do this more efficient? Again, please sorry my english, it´s hard to describe something in a language you don´t speak everyday.

A: 

Inline functions are the normal way to allow the compiler to avoid the overhead of a function call. However, it sounds like premature optimization here, and your efforts would be better spent elsewhere. A code example may help clarify what you want.

Roger Pate
+2  A: 

You just described inline functions (including the function when used rather than jump and return) and references (use the caller's variables rather than copy into the function).

Inline functions just happen automatically when you turn the optimizer on, conditions permitting. Not something to worry about.

References are something you should read about in whatever book you are using to learn C++. They are declared like int foo( int &callers_var ); and can capture things like a field in a matrix.

As Roger said, never optimize until you have a functional program and can verify what is slow. That is the first rule of optimization.

Potatoswatter
A: 

Depending on your target platform (for example, a PC or a Smart Phone), different levels of optimizations may be required. However, from the information provided, I feel strongly that you're seeking unnecessary, wasteful optimizations.

Modern computers work very, very quickly and its not only possible, but probable that your so-called optimizations will yield no return value.

On the other hand, thinking about these topics serves as a good academic exercise to learn more of the ins and outs of programming.

Kivin