Thread: How to execute a top priority function?

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    How to execute a top priority function?

    I'm still an amateur.
    Is it possible to write a program such that it will automatically execute the function in queue base on their priority even if user programmed it randomly?

    Code:
    function1(){ very_important;  }
    function2(){ important; }
    function3(){ less_important; }
    
    int main() {   // Programmed by user
        function3;
        function1; 
        function2;
    
    //Output: Program will execute function1, 2 and lastly 3. 
    }
    Last edited by zenniz; 03-14-2013 at 01:27 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm still an armature.
    Electrifying personality, I'm sure!! < ROFL! >

    Yes, using function pointers as part of a struct. One struct member is the priority, and you would sort the structs with that priority member as the key.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by zenniz View Post
    I'm still an amateur.
    Is it possible to write a program such that it will automatically execute the function in queue base on their priority even if user programmed it randomly?
    Not in the way you've described it, no. The whole point of programming is to do it systematically. If a statement calling function3() precedes a statement calling function1() - within the same block - then function3() is called first.

    Adak has mentioned a way to achieve a comparable effect. However, that requires the programmer to deliberately and systematically set up a scheme by which functions are inserted into a queue, and that queue is sorted so the "highest priority" function in that queue is retrieved and executed first. It is not done by programming "randomly".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP! How do I execute a function in a switch statement??
    By kkmoslehpour in forum C Programming
    Replies: 34
    Last Post: 10-13-2011, 08:40 PM
  2. How to execute a function with a certain probability
    By siddhabathula in forum C Programming
    Replies: 4
    Last Post: 09-10-2010, 07:56 PM
  3. Using the exec call function to execute an app
    By it01y2 in forum C Programming
    Replies: 5
    Last Post: 12-20-2009, 01:08 PM
  4. Having only one function run at higher priority
    By ulillillia in forum C Programming
    Replies: 35
    Last Post: 02-24-2008, 06:56 PM
  5. Passing a function name as a variable to execute it?
    By Zuul in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2002, 12:42 PM