-1

I'm trying to code this relatively simple code in which I want to be able to change the value of set_current. The error message: "Argument type 'void' is incomplete" keeps showing up and I'm not sure why.

I am not an experienced coder but I have to solve this problem for work. I really hope you can help me.

void setCurrent(float set_current);

int main () { 
    printf("%i", setCurrent(0));
    printf("/n/r");
}

void setCurrent(float set_current){
    float v_set_cur = 1.25 + (ILIM_K_USE*set_current);

"Argument type 'void' is incomplete" shows up on the printf line. When I remove the 0 then it works but I want to be able to change that number. What am I missing here?

Lilly S
  • 27
  • 1
  • 1
  • 11
    `setCurrent` returns type `void`. You're passing that as an argument to `printf`. – Thomas Jager Jun 06 '19 at 18:53
  • 5
    Also, tag only the language used. C or C++? – Christian Gibbons Jun 06 '19 at 18:53
  • 1
    By "remove the 0" I assume you mean `printf("%i", setCurrent);`. If that compiles without warnings, I suggest you try to turn up warning level in your compiler. For *gcc* and *clang*, `-Wall -Wextra` is good, for MSVC `/W4` or `/W3`. Because you are printing pointer with `"%i"`, which is generally not what you want to do. – hyde Jun 06 '19 at 18:57
  • [Handy reading](https://stackoverflow.com/a/1044240/4581301) on the nature of `void` – user4581301 Jun 06 '19 at 18:58

5 Answers5

5

You've declared setCurrent as returning void (i.e., nothing), yet in printf("%i", setCurrent(0)); you're expecting it to return an int. One of those things needs to be changed.

jwodder
  • 54,758
  • 12
  • 108
  • 124
4

What am I missing here?

You are missing that the function setCurrent needs to return a value that you can print. In other words - the function definition shall not be void.

My guess is that you want:

float setCurrent(float set_current);  // Notice float instead of void

int main () { 
    printf("%f", setCurrent(0));      // Notice %f instead of %i
    printf("/n/r");
}

float setCurrent(float set_current){                    // Notice float instead of void
    float v_set_cur = 1.25 + (ILIM_K_USE*set_current);  // Calculate value
    return v_set_cur ;                                  // Return the value
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
3

When you call setCurrent, nothing is being returned. A void function inherently does not return a value. Based on your printf statement, you are expecting an integer from calling setCurrent. It appears you want to return a float because v_set_curr is a float.

float setCurrent(float set_current);

int main () { 
    printf("%f", setCurrent(0));
    printf("/n/r");
}

float setCurrent(float set_current){
    float v_set_cur = 1.25 + (ILIM_K_USE*set_current);
    return v_set_cur;
}
Reid
  • 31
  • 4
2

void return type does not return any value; so print operation you should do inside setCurrent function.
Try this code

void setCurrent(float set_current);

int main () { 
    setCurrent(0);
    printf("/n/r");
}

void setCurrent(float set_current){
    float v_set_cur = 1.25 + (ILIM_K_USE*set_current);
    printf("%.2f", v_set_cur);
} 
  • 1
    I recommend against this. A function should do as close to one thing as possible. A `set` function should set. A `print` function should print. Combining responsibilities tends to make a function less usable and more difficult to verify. – user4581301 Jun 06 '19 at 19:02
-2

I experienced the same problem. But THE SOLUTION is that whenever you want to declare a function with void as output, it is better to use only void as input rather than any other data type.

void is not a complete data type. Because a data type gives the form to which variable is to be stored. But void means nothing. Its like when you have no form to which variable to be stored to return a value than how can you expect a function to return a value...

halfer
  • 19,824
  • 17
  • 99
  • 186