for loop that calls multiple functions

This is a discussion on for loop that calls multiple functions within the C++ Programming forums, part of the General Programming Boards category; Hi there, I just wanted to ask if the plan I had for this code solution was even possible before ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    27

    for loop that calls multiple functions

    Hi there, I just wanted to ask if the plan I had for this code solution was even possible before i start figuring out how to do it.

    the program has a [12][7][7] uninitialized array and will populate it using for loops, but each of the 12 [7][7] sections will have a different pattern in them. will it be possible to create a for loop that as it moves through the [12] section calls a different function each time?

    I'm pretty sure it could be done using 'if' and 'else if', but I'm not sure if functions can be called like that? I'm also not sure if there would be a more streamlined way to do it such as changeing the name of the function called depending on the value of k

    I'm not sure if I've explained that very well, so just say if it needs clarifying. Bellow is a bit of pseudo code to try and better explain it

    Code:
    //using if/else if
    
    for (k=0; k < 12 ; k++)
    
    if k=0 (function1() )
    
    else if k=1 (function2() )
    
    else if k=2 (function3() ) etc
    
    functions defined here
    Code:
    //using method I'm pretty sure wouldn't work
    
    for (k=0; k < 12 ; k++)
    
    function(k) ()

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    The first method looks ok (as long as you realize it's pseudo-code), and I don't understand what you're doing in the second method. A better way might be to use a switch statement:
    Code:
    for (k=0; k < 12 ; k++)
    {
        switch(k)
        {
            case 0:
                function1();
                break;
            case 1:
                function2();
                break;
            case 3:
                function3();
                break;
        }
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    thanks for the replies, since I'm new to C++ I wasn't sure if what I was thinking was something I could do.

    what I was trying to show in the second pseudo block was a loop where the name of the function called would change with the value of k. Like I said, I'm pretty sure this wouldn't work, but I thought I'd check.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting source into multiple files(Linux & make)
    By IceDane in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 07:31 AM
  2. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  3. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  4. Replies: 4
    Last Post: 03-11-2005, 04:45 PM
  5. Declaring multiple functions
    By samuel in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2001, 12:15 AM

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