too few arguments to function ‘peachy’

3910 views c
6

I've been trying to practice working with arguments and functions but I keep getting a "too few arguments error" on this basic attempt. Can anyone point out to me what I need to do in order to get this to compile?

#include <stdio.h>
#include <stdlib.h>

int peachy(char* str, int a, int b)
{
    str = "g";
    a = 7;
    b = 6;
    printf("Character: %s\n", str);
    printf("First Integer: %d\n", a);
    printf("Second Integer: %d\n", b);

}


int main(void)
{
    peachy();

}

answered question

Q: Can anyone point out to me what I need to do in order to get this to compile? A: Uh, pass "str", "a" and "b" as arguments to peachy()? You might also want to return a value from peachy(), or change the signature to "void"...

Arguments are expected to be provided by the caller, not hardcoded into the function itself. That's the whole point of them being... well... arguments, and not regular function-local variables.

@paulsm4 Alternatively remove the function params and just leave it void, since youre not using them now anyways.

It's cool man, everything's peachy();

sometimes we have to remember that for beginners some things that are 'obvious' are in fact not 'obvious'. When I learned fortran I could not understand how you could write complex programs, I though it all had to fit on one line. Then somebody said , "you can have as many lines as you need" - aha!

1 Answer

4

like this

#include <stdio.h>
#include <stdlib.h>

void peachy(char* str, int a, int b)
{
    printf("Character: %s\n", str);
    printf("First Integer: %d\n", a);
    printf("Second Integer: %d\n", b);

}


int main(void)
{
    peachy("g", 7, 6);
    peachy("foo", 42, 43); //just to show the use of function args    
}

posted this

Have an answer?

JD

Please login first before posting an answer.