Like Tree11Likes

need help calling a function, please.

This is a discussion on need help calling a function, please. within the C Programming forums, part of the General Programming Boards category; I do not understand how to call a function. I need to call functions "insert", "remove", and "display." Any help ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    99

    need help calling a function, please.

    I do not understand how to call a function. I need to call functions "insert", "remove", and "display." Any help would be much appreciated. Thanks.
    Code:
    #include <stdio.h>
    
    
    void insert(float x, int p, int n, int d, int q);
    void remove(float x, int p, int n, int d, int q);
    void display (float x, int p, int n, int d, int q);
    
    
    
    
    int main(void)
      {   
      int pennies, nickels, dimes, quarters, p,n,d,q;
      pennies=0;
      nickels=0;
      dimes=0;
      quarters=0;
      float x = pennies + 5*nickels + 10*dimes + 25*quarters;
            x =  (float)x/100;
    
    
    
    
    printf( " %d quarters + %d dimes + %d nickels + %d pennies = $%d \n", quarters, dimes, nickels, pennies, x);
    printf("insert");
    scanf("%d%d%d%d", &p, &n, &d, &q);
    pennies += p;
    nickels += n;
    dimes += d;
    quarters += q;
    void insert (float x + p + (5*n) + (10*d) + (25*q));
    x = float x/100;
    printf( " %d quarters + %d dimes + %d nickels + %d pennies = $%d \n", quarters, dimes, nickels, pennies, x);
    
    
    
    
    printf("remove");
    scanf("%d%d%d%d", &p, &n, &d, &q);
    pennies -= p;
    nickels -= n;
    dimes -= d;
    quarters -= q;
    void remove ( float x - p - (5*n) - (10*d) - (25*q));
    x = float x/100;
    printf( " %d quarters + %d dimes + %d nickels + %d pennies = $%d \n", quarters, dimes, nickels, pennies, x);
    
    
     }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    But you do understand how to call a function, you do so every time you use printf or scanf. They are functions provided by the standard C library. The format is
    Code:
    function_name(param1, param2, param3);
    // For example:
    insert(x+ p + (5*n) + (10*d) + (25*q));
    You need to remove the return type (i.e. the "void") and the parameter type (i.e. the "float"), from the calls on lines 31 and 44. They are only used in function declarations and definitions.
    rags_to_riches likes this.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Am I moving in the right direction? I changed a lot. Thanks.
    Code:
    #include <stdio.h>
    
    
    insert (x + p + (5*n) + (10*d) + (25*q));
    remove (x - p - (5*n) - (10*d) + (25*q));
    display (p, n, d, q, x);
    
    
    
    
    int main(void)
      {   
      int pennies, nickels, dimes, quarters, p,n,d,q;
      pennies=0;
      nickels=0;
      dimes=0;
      quarters=0;
      float x = 0
    
    
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d \n", pennies, nickels, dimes, quarters, x);
    printf("insert");
    scanf("%d%d%d%d", &p, &n, &d, &q);
    insert("%f, %d, %d, %d, %d", x, p, n, d, q);
    display("%d, %d, %d, %d, %f", p, n, d, q, x );
    
    
    
    
    printf("remove");
    scanf("%d%d%d%d", &p, &n, &d, &q);
    insert("%f, %d, %d, %d, %d", x, p, n, d, q);
    display ("%d, %d, %d, %d, %f", p, n, d, q, x);
     }

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,265
    Now you've ruined the function declarations/definitions.

    Code:
    int sum(int x, int y);  // <-- function declaration (note semicolon)
                            //     return type:  returns an integer
                            //     argument list:  accepts two integers
    int main(void)
    {
        int total;
    
        total = sum(3,4);   // <-- function call
                            //     return type not included in function call
                            //     arguments sent that match the argument list
                            //        in the function declaration and definition
    
        return 0;
    }
    
    int sum(int x, int y)  // <-- function definition (note no semicolon)
    {
        return x + y;
    }
    Or, alternatively, you can define the function above "main()" and leave out the explicit declaration:

    Code:
    int sum(int x, int y)  // <-- function definition (note no semicolon)
    {
        return x + y;
    }
    
    int main(void)
    {
        int total;
    
        total = sum(3,4);
    
        return 0;
    }
    anduril462, cfanatic and Ashl7 like this.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    No, not every function takes a string of format specifiers. Also, your function prototypes were working before, but you changed them to function calls.

    Lines 4 and 5 (in post #3) are examples of you correctly calling the insert and remove functions, except you must call a function from within another function.

    My point was, you know how to call functions. Note, you have never had to put a type (like int, void, float) before printf or scanf when you use them. Nor have you ever had to put a type like char * before the parameters.

    This is wrong (the red stuff doesn't belong):
    Code:
    int printf(char * "Notice the incorrect return type and parameter types, they don't belong!\n");
    The following is the correct way. This is how you have been using printf and scanf all along.
    Code:
    printf("Notice, this is correct, there is no return type or parameter types.\n");
    Look at the example in my previous post, I showed you exactly how to call the insert function. remove and display are similar.

    I think you need to read some tutorials. We have one here: Functions in C - Cprogramming.com. You should also Google for several others too, to get different viewpoints and a more complete picture.

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    593
    Your prototypes should have return types and parameter types.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Ok. I changed it again. I have been looking on google and at my notes and in my book for over 5 hours today, and I am still not getting it. I foolishly took this class online and help is impossible to get. Thanks again.
    Code:
    #include <stdio.h>
    
    
    int insert (int p, int n, int d, int q, float x)//
    {
    	return x + p + (5*n) + (10*d) + (25*q);
    }
    int remove (int p, int n, int d, int q, float x)//
    {
    	return x - p + (5*n) - (10*d) - (25*q);
    }
    int display (int p, int n, int d, int q, float x)//
    {
    	return p, n, d, q, x;
    }
    
    
    int main(void)
      {   
      int pennies, nickels, dimes, quarters, p,n,d,q;
      pennies=0;
      nickels=0;
      dimes=0;
      quarters=0;
      float x = 0;
    
    
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%f \n", pennies, nickels, dimes, quarters, x);
    printf("insert");
    scanf("%d%d%d%d", &p, &n, &d, &q);
    insert("%f, %d, %d, %d, %d");
    display("%d, %d, %d, %d, %f");
    
    
    
    
    printf("remove");
    scanf("%d%d%d%d", &p, &n, &d, &q);
    insert("%f, %d, %d, %d, %d");
    display ("%d, %d, %d, %d, %f");
    
    
    return 0;
     }
    Ashl7 likes this.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    cfanatic-
    can i not use the layout i have above?

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,265
    Quote Originally Posted by rosemary View Post
    cfanatic-
    can i not use the layout i have above?
    If you're referring to putting the function definitions above "main()", you can - see my post #4 above.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    matticus-
    could you help me understand what is wrong with these two lines? thanks.

    Code:
    insert("%f, %d, %d, %d, %d");
    display("%d, %d, %d, %d, %f");

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    Quote Originally Posted by rosemary View Post
    matticus-
    could you help me understand what is wrong with these two lines? thanks.

    Code:
    insert("%f, %d, %d, %d, %d");
    display("%d, %d, %d, %d, %f");
    Here, you are passing one parameter -- a string -- to insert and display. Those functions don't know what to do with that string, nor do they know what %f and %d mean.


    Those functions each take 5 parameters (4 ints and a float). You need to pass in the correct integer and float values, in the correct order. Change line 31 and 39 to look like this:
    Code:
    insert(pennies, nickels, dimes, quarters, x)
    Note, your display function should actually display something. Try using printf inside that function, to print out the value of p, n, d, q and x:
    Code:
    printf("p = %d\n", p);
    printf("n = %d\n", n);
    ...
    Matticus likes this.

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    2,265
    Thanks anduril!

    @rosemary, if you're really interested in learning and not just getting a good grade somewhere, I'd recommend you play around with some small, simple programs using functions to get the hang of it. The code I provided in post #4 is a fully working program (though with no output). That might help you see how all the details of a function works. Then mix up the code a bit, seeing if you can make another function that does multiplication of two numbers, etc. Writing several small, simple programs exercising concepts you're learning will give you a great understanding of those concepts. Then you can scale up the programs to deal with more [relatively] complicated functions as you're doing now.
    Ashl7 likes this.

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    matticus-
    actually, i did this assignment already without functions, and it worked, but the professor wanted it with functions. it is too late for me to turn it in, i really want to understand how functions work. i truly have been working on this for roughly 6 hours today. i agree, i need to understand more basic concepts first, but this is what he is expecting me understand, so i am "trying" to figure it out. thanks again for all your help.

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    I still can't get it to work. This is where I am at. Thanks everyone.
    Code:
    #include <stdio.h>
    
    
    int insert (float x, int p, int n, int d, int q)//
    {
    	return (x + p + (5*n) + (10*d) + (25*q))/100;
    }
    int remove (float x, int p, int n, int d, int q)//
    {
    	return (x - p - (5*n) - (10*d) - (25*q))/100;
    }
    int display (int p, int n, int d, int q, float x)//
    {
    	return printf("p = %d\n", p);
    		   printf("n = %d\n", n);
    		   printf("d = %d\n", d);
    		   printf("q = %d\n", q);
    		   printf("x = %f\n", x);
    }
    
    
    int main(void)
      {   
      int pennies, nickels, dimes, quarters, p,n,d,q;
      pennies=0;
      nickels=0;
      dimes=0;
      quarters=0;
      float x = 0;
    
    
    printf( " %d pennies + %d nickels + %d dimes + %d quarters = $%d\n", pennies, nickels, dimes, quarters, x);
    printf("insert");
    scanf("%d%d%d%d", &p, &n, &d, &q);
    insert(pennies, nickels, dimes, quarters, x);
    display(pennies, nickels, dimes, quarters, x);
    		
    printf("remove");
    scanf("%d%d%d%d", &p, &n, &d, &q);
    remove(pennies, nickels, dimes, quarters, x);
    display (pennies, nickels, dimes, quarters, x);
    
    
    return 0;
     }

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    I have been looking on google and at my notes and in my book for over 5 hours today,
    Banging your head against a concept rarely works. For example I'm crap at writing essays or other term papers, but I don't spend hours agonizing over them per day. It really is a rather poor study habit for anything.

    Functions are actually rather simple. We have a tutorial, my goal is to present you with enough information to make sense of the tutorial, and with any luck I can do that by breaking down one function for you.
    Code:
    void insert(int x, int p, int n, int d, int q);
    Really, if you want to understand calling functions you really only need to understand what the the prototype can tell a person.

    The very first word, void, is the type of the value returned to the caller. Unless the return type is void, a variable should be on the left hand side of the assignment operator to store the value. (This is the easiest expression to teach to handle return values. The other way is actually pretty dependent on context and I don't want to teach context.)

    Then the next word is insert, the name of the function.

    Then you have the parameter list in parentheses. Each of the parameters should receive an argument of the same type, whether that is a variable name or other expression resulting in a value of the appropriate type. So you must pass in five integers to complete this function call. You have to copy the parentheses. The other five integers will probably look unlike the integers in the parameter list, and this is normal. The parameter list is a visual reference for you, and a technical reference for the compiler. It will use the parameter list to enforce various rules regarding the C type system and function invocation.

    The white space can be used to determine what part is what in the future. If you proceed from left to right when you read, you can pick apart all the parts of the function. Once you know what all the parts are, you need to write the statement to make the call. Write out all the parts and conclude the statement with the semicolon.

    That is not all there is to know. You still have yet to understand the difference between pass by value semantics and reference semantics, which is an important omission. But you do not have to understand pass by reference semantics until you start the unit on pointers.

    Functions in C - Cprogramming.com

    I still can't get it to work. This is where I am at.
    It looks like you aren't assigning the return value of any of your functions to x.
    Last edited by whiteflags; 10-11-2012 at 07:31 PM.
    rosemary and Ashl7 like this.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-26-2011, 04:44 AM
  2. Calling a function inside a function
    By kizyle502 in forum C Programming
    Replies: 1
    Last Post: 09-17-2009, 11:29 AM
  3. Replies: 15
    Last Post: 06-09-2009, 02:19 AM
  4. Calling Cdocument function in Cview function
    By RancidWannaRiot in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 12:09 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM

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