Thread: Function Calls and Returned Values...Newbie Needs Help

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Function Calls and Returned Values...Newbie Needs Help

    Hello everyone, as you guessed, I am a C programming student and was hoping someone could lead me in the right direction. I am not looking for THE answer, just a confirmation that I am on/off the right path... I am trying to use a value returned from one function in another function. I believe I have to assign the returned value to a variable name so I can use this newly named variable in the next function. I have tried to use the function header of the first function (since it returns the value I need) but the returned value for the second function was way off. All help is appreciated!

    dno117

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, sounds good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Basically, something like this?
    Code:
    result1 = bar();
    result2 = foo( result1 );
    And by function header, you mean a function call as so?
    Code:
    result = foo( bar() );
    (contrastingly a header is a file containg function prototypes or signatures (i.e. name, return type, and parameter list) )

    Both should have the exact same effect. If they don't paste a code-snippet so we can see what's going on.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Thank you for your replies. Please excuse the "function header/function call" mix-up, I am a beginner. Below you will find my code. The first function "getTaxAmount" works fine, as it multiplies the "fPurchaseAmount" input by with the tax rate for "fDelMar" (Del Mar location). I cannot get the second function "addTaxAmount" to add the value returned by the "getTaxAmount" function to the "fPurchaseAmount" value...


    Code:
    #include <stdio.h>
    
    float getTaxAmount(float, float);
    float addTaxAmount(float, float);
    
    
    main()
    {
    
        float fPurchaseAmount;
        float fDelMar = .0725;
        
       
        printf("\nEnter Purchase Amount: $ ");
        scanf("%f", &fPurchaseAmount);
       
        printf("\nThe Tax Amount for Del Mar is $%.2f\n", getTaxAmount( fPurchaseAmount, fDelMar) );
        printf("\nThe Total Amount With Tax for Del Mar is $%.2f", addTaxAmount( getTaxAmount, fPurchaseAmount) );
        
        getch ();
       
    }
    float getTaxAmount( float operand1, float operand2)
    {
        return operand1 * operand2;
    }
    
    float addTaxAmount( float operand1, float operand2)
    {
        return  operand1 + operand2;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Store the result of getTaxAmount in a variable, which you can then
    - print
    - pass onto the next function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Or you could just supply getTaxAmount with arguments when you call it as an argument to addTaxAmount (I wouldn't think you could have compiled it otherwise):

    Code:
    printf("\nThe Total Amount With Tax for Del Mar is $%.2f", addTaxAmount( getTaxAmount( fPurchaseAmount, fDelMar), fPurchaseAmount));
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Thanks Everyone for your help and guidance...I will plug away at it when I get home from work!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    #include <stdio.h>
    // Forgot to include conio.h
    
    // Don't remove parameter names from the prototypes.
    float getTaxAmount(float, float);
    float addTaxAmount(float, float);
    
    // Don't use implicit main
    main()
    {
    
        float fPurchaseAmount;
        float fDelMar = .0725f;
        
       
        printf("\nEnter Purchase Amount: $ ");
        scanf("&#37;f", &fPurchaseAmount);
       
        printf("\nThe Tax Amount for Del Mar is $%.2f\n", getTaxAmount( fPurchaseAmount, fDelMar) );
        // Missing argument list for getTaxAmount.
        printf("\nThe Total Amount With Tax for Del Mar is $%.2f", addTaxAmount( getTaxAmount, fPurchaseAmount) );
        
        // Avoid using, and if you do, include conio.h
        getch ();
       
    }
    float getTaxAmount( float operand1, float operand2)
    {
        return operand1 * operand2;
    }
    
    float addTaxAmount( float operand1, float operand2)
    {
        return  operand1 + operand2;
    }
    A few problems, comments included in red color.
    About implicit main: http://cpwiki.sourceforge.net/Implicit_main
    Float pointer numbers are double by default, unless you add f to the end, and since you are using a float datatype, then you should add f to the end.
    And you must understand that to call a function, you must add ( and ), and within those, you pass the arguments to the function. Otherwise, you take the address of the function, which is not what you want.
    And it's usually considered bad practice to remove the names from the parameters in the function prototypes:

    Code:
    void foo(int); // Bad
    void foo (int x); // Good
    OK, so this isn't the best name for the parameter there, but it's just an example.
    Last edited by Elysia; 10-16-2008 at 02:22 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Not only is main implicit, but it also does not have any arguments specified! In C++ this is all well in good, but in C that is erroneous since main() can only be a function with no parameters or a function with two parameters. In C main() is the same as saying main(...).

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by master5001 View Post
    Not only is main implicit, but it also does not have any arguments specified! In C++ this is all well in good, but in C that is erroneous since main() can only be a function with no parameters or a function with two parameters. In C main() is the same as saying main(...).
    Actually, not really...
    It only matters in the prototype, and not the actual definition. And since main shall have no prototype nor be called from the code, it doesn't really matter.
    But it DOES matter for self-declared functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    True, but I don't think its a good habit to get into. Furthermore I don't believe I ever see you write your main function like that. Not that whether you do or don't makes something right or wrong. The compiler should be able to figure out whats what on its own. That doesn't mean it should have to.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm a C++ dev, so I hate the "void" part in functions
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Alright. We will put this in the C habits die hard for me pile. Even in C++ I put void in prototypes and definitions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM