Loops in functions?

This is a discussion on Loops in functions? within the C Programming forums, part of the General Programming Boards category; Ok thanks to everyone who helped me figure out how to use makefiles. Now that i have, i've got an ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68

    Loops in functions?

    Ok thanks to everyone who helped me figure out how to use makefiles.

    Now that i have, i've got an obsession for converting as much of my code into functions as i can!

    My question is this:

    I use this a lot ( A LOT!)

    Code:
    for (x = 0; x < number; x++)
    {
    	for (y = 0; y < number; y++)
    	{
    		for (z = 0; z < number; z++)
    		{
    		//Some code here
    		}
    	}
    }
    Is it possible to make this a function such as setupxyz( Some Code Here ); ?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Not generally, no. It can be done with advanced techniques such as function pointers. But the best way to do this I believe is to use macros or simply to create a macro in your IDE that pastes that code when you need it.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Oh that sounds cool! It would certainly speed things up a bit! I'm not using an IDE as such, just kate and gcc. How would i go about it? Or is it more to do with the editor?

    Basically is there something i can type that gcc will interpret as the above code or do i just set up a macro in kate?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Macros are a feature of the C language:

    Code:
    #define MyLoop(mycode) \
    for (x = 0; x < number; x++) \
    { \
    	for (y = 0; y < number; y++) \
    	{ \
    		for (z = 0; z < number; z++) \
    		{ \
    			mycode; \
    		} \
    	} \
    }
    
    int main()
    {
    	MyLoop(my_code_here);
    }
    Will translate into your loop with my_code_here inserted into where you want the code in the loop.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Thats great! Thats gonna save me some time and shorten my program!
    Can i put macros in my header file or do they need to be in the main source file?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    They can be anywhere you want.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    Changed it a little bit so now i can insert code wherever i like

    Code:
    #include <stdio.h>
    
    #define setup_xyz(after_x, after_y, after_z) \
    for (x = 0; x < number; x++) \
    { \
    after_x; \
    	for (y = 0; y < number; y++) \
    	{ \
    	after_y; \
    		for (z = 0; z < number; z++) \
    		{ \
    			after_z; \
    		} \
    	} \
    } 
    
    int main()
    {
    int number = 3;
    int x, y, z;
    
    	setup_xyz( printf("\nafter_x"), printf("\nafter_y") , printf("\nafter_z"));
    }
    AND it works when i stick it in my header file!!

    This is honestly going to take about 300 lines out of my code!! Thanks A LOT!!
    Simon.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    It might be my C++ background showing, but I would rather use the core language than resort to the preprocessor if the core language features suffice. In this case I think function pointers may well suffice:
    Code:
    #include <stdio.h>
    
    void run_loop();
    
    void foo()
    {
        printf("Hello world!\n");
    }
    
    int main()
    {
        run_loop(2, foo);
        return 0;
    }
    
    void run_loop(int number, void (*func)())
    {
        int x;
        for (x = 0; x < number; x++)
        {
            int y;
            for (y = 0; y < number; y++)
            {
                int z;
                for (z = 0; z < number; z++)
                {
                    func();
                }
            }
        }
    }
    The trade-off is that you have an extra function call (I do not think function pointers are easily or often inlined).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Aside from the preprocessor, the little more advanced method might be function pointers, but it might just make your code into a big mess with lots of functions. Function pointers are basically pointers that point to a function. So inside that little loop, it can call the function you supply, even with parameters, but it might be that you get a lot of functions all around, so I suppose you can use a mix of both methods.

    Case you're interested in this, I might show some examples.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    I don't even know how to use function pointers! Think i'll stick to macros just now!

    I've already started converting my program over to using macros!

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Macros aren't the safest thing in the world to use and is mainly C, which is why we suggested that you might also use function pointers, because they're very handy too.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not a great fan of macros. Particularly multi-line macros. They make debugging the code horrible - and sooner or later you will be debugging your code, I promise.

    If you really want to do things at the outer, middle and inner loop iterations, then you probably should be writing out the whole code. You can always "copy'n'paste" if you have some code that is similar to what you need this time, then strip out stuff and add the new bits [but beware of "copy'n'paste" bugs that sneak in - because you don't check what all the code does, you end up with "extra" or "missing" bits in the code].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Another tip might be to actually write and debug the code you put into the macro first so you don't screw up. The compiler will complain at your macro, but it can't pinpoint exactly what in the macro is wrong because you see one a single line - the macro name.
    So use them carefully.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Another tip might be to actually write and debug the code you put into the macro first so you don't screw up. The compiler will complain at your macro, but it can't pinpoint exactly what in the macro is wrong because you see one a single line - the macro name.
    So use them carefully.
    Oh, yes, that's another problem with multi-line macros - the compiler says "error in line 532", which just shows you the macro-line with it's arguments. You don't know at all what is actually wrong in there, unless you start looking at the pre-processed code, and then compile that to get the new line-number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Location
    Glasvegas, Scotland.
    Posts
    68
    so i'd be as well just typing it out each time?

    There are a lot of places in my program where i use the above construct. with code sometimes after x or y etc.
    Its not bad programming practice to leave them as is?

    I sort of thought that if i just typed out the entire program, as is in one source file that it would be considered bad practice?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM
  5. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 11:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21