Thread: Functions are killing me, help!

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    6

    Functions are killing me, help!

    I have been trying to figure out the proper use of functions. I sort of understand them, but then again, I sort of don't. I know my calling of them in main is just a disaster, and as simple as this program sounds, it is really frustrating me.

    I'd appreciate any help.

    The purpose: takes two inputs from the user (voltage and resistance) in a function and returns the amp value (voltage / resistance *1000) and then prints it in main. There is also error checking that needs to be done to make sure the input values are greater than zero. Ugh.


    Code:
    #include <stdio.h>
    
    float input(float volts, float resistance);
    float current(float value);
    void display(void);
    
    float input(float volts, float resistance)
    {
            float value, volts, resist;
            printf("Enter a value for voltage and resistance, separated by a space: ");
            scanf("%f %f", &volts, &resist);
            value = (volts/resist);
            return value;
    }
    
    float current(float value)
    {
            float current;
            current=value*1000;
            return current;
    }
    
    void display(void)
    {
            printf("The current is %f", current);
    }
    
    int main(void)
    {
            float current;
            curent=current(value);
            display();
            return 0;
    
    }
    ~
    I am sure I have wrong floats being used and saved into functions..right now I feel as if I have absolutely no clue what I am doing. It sounded so simple when I read it a few days ago, but now =(
    Last edited by DLink89; 01-30-2011 at 11:06 AM. Reason: more info

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    float current
    and current(int)
    ambiguous..the compiler will freak out

    and no value variable declared..

    Code:
    float current(float value)
    {
            float current;
            current=value*1000;
            return current;
    }
    is also incorrect...let the names be unique and it should be grand
    Last edited by Eman; 01-30-2011 at 11:26 AM.
    You ended that sentence with a preposition...Bastard!

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Simply put don't give variables the same name as as function.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    6
    Thank you for the quick replies. I will use your advice and tinker with this quickly and give you an update in a little bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with functions
    By jusfry01 in forum C Programming
    Replies: 2
    Last Post: 05-22-2010, 06:25 PM
  2. Functions calling other functions.
    By kbro3 in forum C++ Programming
    Replies: 2
    Last Post: 12-27-2009, 12:10 AM
  3. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  4. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  5. functions are killing me
    By 77walker in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2004, 03:49 PM