Best way to call a subroutine

This is a discussion on Best way to call a subroutine within the C Programming forums, part of the General Programming Boards category; I am writing a program that uses 20 different case options to modify outportb values. The subroutine converts Hex to ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Md
    Posts
    7

    Best way to call a subroutine

    I am writing a program that uses 20 different case options to modify outportb values. The subroutine converts Hex to decimal, and used often. I don't want to add the subroutine in each case selection. Here is what I was thinking of, but I am not quite sure how to do it. Can I have a separate program after the end of the main program? Or should I use an #include?

    Code:
    main()
    {
    case '1':
    
    // call H2D 
    
    H2D;
    
    break;
    
    case '2':
    
    H2D;
    
    break;
    
    etc.
    
    } // end main
    
    // Hex to Decimal conversion
    
    H2D()
    {
    
    program lines .......
    
    } // end H2D

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    are you asking how to use functions?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    So for each case that needs that convert function, you want to just have the code added:

    convert(whatever parameters you need here);

    Saves a lot of space and helps keep everything organized.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Location
    Md
    Posts
    7
    I am just asking if the syntex look correct.
    I know the function works fine. but is this the correct way to call the subroutine?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    No.
    Code:
    void your_func() { }
    int main()
    {
    	your_func();
    	return 0;
    }
    Always included paranthesis, and pass arguments if needed.
    Your function also lacks return type.

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by N3BPM View Post
    I am just asking if the syntex look correct.
    I know the function works fine. but is this the correct way to call the subroutine?
    i can see u are trying to use switch statment over there. which should be

    Code:
    main()
    {   
           switch (ch)   
          {       
              case '1':          
                     // call H2D            
                     H2D;     
                     break;    
              case '2':   
                    H2D;  
                     break; 
              etc.   
         }
    }
    ssharish

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Quote Originally Posted by N3BPM View Post
    I am writing a program that uses 20 different case options to modify outportb values. The subroutine converts Hex to decimal, and used often. I don't want to add the subroutine in each case selection. Here is what I was thinking of, but I am not quite sure how to do it. Can I have a separate program after the end of the main program? Or should I use an #include?

    Code:
    int main()
    {
    	switch(something)
    	{
    		case '1':
    			// call H2D 
    			H2D();
    			break;
    		case '2':
    			H2D();
    			break;
    			//etc.
    
    	}
    } // end main
    
    // Hex to Decimal conversion
    
    void H2D()
    {
    	//program lines .......
    } // end H2D
    Come on, it's not like we read gibberish that doesn't even ident. Don't forget a closing bracket for your switch.
    main should ALWAYS return int. See FAQ.
    Function calls is name_of_function(arguments);
    Yes, you also forgot to add a switch statement to switch for something.
    All functions need to return a value (if not, use void). I highlighted things I added in red.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You didn't specify whether anything different was happening for each of those cases, so if they all do the same thing, you could save time & space by doing this:

    Code:
    switch( ch )
    {
    case '1':
    case '2':
    case '3':
    case '4':
        H2D();
        break;
    
    default:
    ...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. minix system call pls help for project
    By porvas in forum Linux Programming
    Replies: 2
    Last Post: 06-14-2009, 03:40 AM
  2. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  3. Error C2664 - Trying to call an external Dll
    By jamez05 in forum C++ Programming
    Replies: 3
    Last Post: 08-08-2006, 07:07 AM
  4. Iterative Tree Traversal using a stack
    By BigDaddyDrew in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2003, 05:44 PM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 06:06 PM

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