-1

I already saw a question about it, more than one, but it did not really solve my problem. The code is bigger, but the problem is with this part. I have this thing:

int globalx=12;  
int globaly=10;

void Function (int measurex, int measurey)
    float thingx()
            {
            (((globalx/2)-measurex)/(globalx/2));
            }
    float totalx=globalx/(float)thingx;
    float totaly=globaly/2;

and there is something wrong with it that returns that error. globalx and globaly are,as the name suggests, two global variables. What am I doing wrong here? How can I write something that does what I do intend to do here?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Lex
  • 1
  • 1
  • 2

3 Answers3

1

A couple of things about your code:

  • Replace thingx with thingx().
  • Function definition should be inside braces {...}.
  • Define thingx() outside Function definition (compiler dependent, since Nested functions are not allowed in standard C).
  • Add a return statement to the thingx() function.
  • Add int measurex as an argument to thingx() function.
  • In Function, after getting the values of totalx and totaly you are not doing anything with them (maybe you just didn't put the whole code of this function in your question).

Your functions definitions should look like this:

float thingx(int measurex)
{
    return (((globalx/2)-measurex)/(globalx/2));
}

void Function (int measurex, int measurey) {
    float totalx=globalx/(float)thingx(measurex);  // <-- missing parentheses, should be thingx(...)
    float totaly=globaly/2;

    // You probably want to do something with totalx and totaly here...
}

main:

int globalx=12;  
int globaly=10;

Function(globalx, globaly);

Also, keep in mind that this will truncate the result to an integer globaly/2 since globaly is defined as int (You can read this about integer division: What is the behavior of integer division?)

Community
  • 1
  • 1
marian0
  • 664
  • 6
  • 15
0

Your code is really messed up. For your function you need to have all your code inside of braces {}. Like this...

void myFunction(int a, int b)
{
    //put your code in here
}
marian0
  • 664
  • 6
  • 15
Neil Roy
  • 603
  • 5
  • 13
0

The error you're seeing, because, thingx() being a function, thingx is basically a function pointer, and you're trying to perform the division of a float with a function pointer.


First, let me declare clearly, Nested functions are not standard C. They are supported as GCC extension..

Having said that, in your case, you need to

  1. Move the thingx() function definition outside of Function().

  2. Just like you did for thingx() function, the body of Function() should also be inside {...}.

  3. Use the function call operator () to call a function. So basically your statement should look like

    float totalx=globalx/(float)thingx(measurex);
    

    ad the function definition should look like

     float thingx(int measurex){..
    
  4. Add a return statement to your function. As per the C11 standard, chapter §6.9.1

    If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

    so, the function body should look like

    {
        return (((globalx/2)-measurex)/(globalx/2));
    }
    
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261