Thread: Function call another function

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    19

    Question Function call another function

    How do you call a function in a function?

    This is the function:
    Code:
    int comStaRatio(int comments, int statements)
    {
        int ratio=1;
        
        ratio=comments/statements;
        
        return(ratio);
    }
    comments and statements will be two other functions both returning int values.
    So when called I will do:
    Code:
    printf("Comment to statement ratio: %d\n", comStaRatio(comCount(file_pointer), staCount(file_pointer)));
    but this doesn't work. Why?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    I'm unsure, but it returns 0.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm unsure, but it returns 0.
    That sounds okay, if the result of comCount(file_pointer) is less than the result of staCount(file_pointer).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Code:
    printf("Comment to statement ratio: %d\n", comStaRatio(comCount(file_pointer), staCount(file_pointer)));
    First you are missing your second argument to printf. How are you to use staCount()?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well what are the values of comments and statements in the function?

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    crap never mind my last post!

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    After actually looking more closer, your %d qualifier should be %f.

    If your division is less than 0, that is what you will get. What level of precision are you needing with this calculation?

  9. #9
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    laserlight you are a legend! I can;t believe i was so stupid not to realise that the answer actually is 0!!!

    Thank you!

  10. #10
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    Uh oh. Whenever I try to change the function and the function call to float it doesnt work!
    Code:
    float comStaRatio(int comments, int statements)
    {
        float ratio=1;
        
        ratio=(float)comments/(float)statements;
        
        return(ratio);
    }
    Call:
    Code:
    printf("Comment to statement ratio: %.2f\n", comStaRatio(comCount(file_pointer), staCount(file_pointer)));
    Any ideas?

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Code:
    printf("Comment to statement ratio: %.2f\n", comStaRatio(comCount(file_pointer), staCount(file_pointer)));
    See something that dosent match?

    Edit: Ignore it does >_<
    Last edited by mike_g; 03-06-2008 at 12:21 PM.

  12. #12
    Registered User
    Join Date
    Mar 2008
    Posts
    19
    No I can't say that I do.
    The two parameters you have in green are ints yes but comStaRatio returns a floating point number.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Uh oh. Whenever I try to change the function and the function call to float it doesnt work!
    It looks okay to me. I suggest that you post the smallest and simplest compilable code that demonstrates the problem.

    See something that dosent match?
    Nope.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >See something that dosent match?
    No, I don't. Perhaps you could elaborate on what you think is wrong.
    My best code is written with the delete key.

  15. #15
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yeah I edited my post. I just read the brackets wrong o_0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM