Hello :)
I just wrote a big program(for me it seems big) and wonder if I should divide instruction blocks in separate function or leave all in the main function. With everything being in the main function, the code looks a little hard to read with a lot of for's and if's.
If dividing the code in more functions, I have either to use global variables or to pass the required variables through reference or pointers to the function. From what I've read, global vars are not a good idea, for a variety of reasons. Also, passing by reference/pointer would also create new variables and use therefore more memory (?).
So it seems I have to choose between an odd looking code and less memory consumption and a good looking code and more memory consumption. What would be your advice ?
The primary consideration should always be human readability. Don't worry about memory consumption or performance unless and until you absolutely have to. This is the first rule of program optimization.
That said, if the code is getting hard to read as you say, then bust it up into functions.
You are worried about something like 1kb of excess used memory max. Also do not forget that compilers are pretty good at optimizations. So your worries about speed and memory consumptions are pointless.
Try to minimize passed variables. Prefer passing primitive types by value and complex by const reference. Avoid modifying passed values if you can. Prefer functions with single return type. If you need to return two values, use std::pait/tuple or gather your values in struct. Or just make a class which will handle invariants by itself.
don 't worry about performance. this still just a console application. when you will write games with 3d graphics you should worry about performance.
but in my opinion use functions. i used functions in my code look at it . if it was all in the main function it would be confusing and unreadable even if i coded it.
imagine this code without functions !
so i suggest to use functions in your programs and don 't worry about performance or memory.
Also, the process of breaking down a procedure into discreet logical functions will often clarify the solution and make you restructure it in a way which is inherently more efficient.
is there an advantage of using pointers over references
Note that there is three different entity which can be represented as pointer:
Pointer to object: You can pass a null pointer to denote that there is no object (there is better ways and anyway you should use smartpointers instead)
Pointer to array first element: only way to pass array of undetermined size in function (better to pase safer containers by [const] reference)
Iterators to array elements (use containers and supplied iterators instead)
Generally you should use pointers when you have to. Use references and value semantic otherwise.
Thanks for the opinions.
Fr0zen1, you said that the compilers usually replace a reference with the actual object. That means that no additional variables are created ?
I'll split the code in functions than. Just to be sure, is there an advantage of using pointers over references ? I would prefer to use references because they just look better and easier to use.
I know that creating new vars won't affect the performance, since I only handle ints, strings, and chars. But I just want to build healthy programming habits.
*Edit: I deleted my previous post, because new posts were written while I was writing it.
Thanks MiiNiPaa, understood.
Modern compilers pass arguments via registers (although this is machine dependent), so as others have said worrying about memory usage is pointless.
If as you say this is a "big" program and it is all in main, then yes, it absolutely should be broken up.
1) Look for blocks of code that are repeated. If you do the same thing more than once, then those lines are candidates to make a function.
2) Look for blocks of code that have a concise purpose. Moving these lines (even if they aren't repeated) to a well named function makes it clearer what your program is doing.