3
int getPositionsH(int r, int ans, int size){
    int x=0;
    int y=0;
        if (ans==9){
            x =0;
        } else
            x=(rand()%size);

        y=(rand()%10);  
    return x;
    return y;
}

Basically this is a function in c that is supposed to return 2 randomly generated positions x and y. However while debugging I noticed that x and y are still empty after this is executed. No idea why because I wrote return and everything. Any ideas why? Any help is appreciated.

P.P
  • 117,907
  • 20
  • 175
  • 238
chris
  • 137
  • 2
  • 11
  • Now its not returning anything?? – Jibin Balachandran Dec 12 '15 at 13:23
  • 2
    You should take a look at passing in 2 pointers, one for x and one for y. As others have pointed out, you can only return a single value from a function. In C, I've found that the returned value is typically used for error checking. To return values that you want, you pass in a memory address to the function, then apply your function code and store the result at the memory address. When the function completes, the stored value at the memory address remains the same and is unaffected by change in scope. – NathanielJPerkins Dec 12 '15 at 13:52

4 Answers4

4

A function can return only one value in C. As it is, the function returns only x and the statement return y; has no effect -- it's unreachable code.

If you want to return multiple values, you can either pass pointers and return the values in their content or make a struct to and return values.

typedef struct position {
   int x;
   int y;
}pos_type;

pos_type getPositionsH(int r, int ans, int size){
    pos_type p;
    int x=0;
    int y=0;
        if (ans==9){
            x =0;
        } else
            x=(rand()%size);

        y=(rand()%10);  

    p.x = x;
    p.y = y;
    reutrn p;
 }

and in the caller:

pos_type t = getPositionsH(...);

int x = t.x;
int y = t.y;
.....
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
P.P
  • 117,907
  • 20
  • 175
  • 238
  • Returning structures is only allowed in the last C standards, It's better to pass a struct pointer to return several values and fill the struct with the proper data. – Luis Colorado Dec 14 '15 at 05:51
  • @LuisColorado While it's generally good to return data in a pointer (instead of copying around), it's OK for small structs. Are you saying returning a struct by value is not allowed in C11? Can you cite some links/references for this claim? – P.P Dec 14 '15 at 20:42
  • I was referencing standards previous to C11. For a cite, just read K&R. Not all compilers are C11 compliant. C11 is one of the last C standards I meant in my comment, and as such, it was only a comment. C11 is the only C standard you know/use? – Luis Colorado Dec 15 '15 at 09:12
  • @LuisColorado No. You said "only allowed in the last standards". So I assumed it's C11. If you are going to talk about a specific standard, why not explicitly state which one. If not C11, then please specify which in standard it's invalid. Unless you can cite a proper reference, your claim is baseless and wrong. – P.P Dec 15 '15 at 09:52
  • Kernighan&Ritchie's book "The C programming language" in it's first edition, forbids to return structs as return values or being passed as parameters by value. Further pre-ansi standards also forbid and I don't remember if first ANSI C standard allows or forbids it expressely. But my first interest in this question is to clarify something to the original questioner, who can be using a different compiler than you use. Not in discussing here with you. – Luis Colorado Dec 15 '15 at 10:13
  • @LuisColorado K&R first edition was outdated even by mid-80s (But you want "compatible solution" with it in 2015). None of the C standards (since C89) forbid this. So I don't see any reason to even mention that a certain code construct is invalid just in K&R1. C has changed a lot since since 1978. I doubt anyone is using a compiler that's 40 years old, let alone a newbie learning C in 2015. – P.P Dec 15 '15 at 10:29
1

you can't return two values this way.

int getPositionsH(int r, int ans, int size)

being declared as int return value will return only a single int

 return x;
 return y;

After returning x, program execution will return from the function, thus return y will remain unreachable.

shakhawat
  • 2,639
  • 1
  • 20
  • 36
0

Use pointers for x and y. Instead of returning values, the function is setting the values:

void getPositionsH(int r, int ans, int size, int *x, int *y) {
    *x = ans == 9 ? 0 : rand() % size;
    *y = rand() % 10;
}

to be called

int x,y;
getPositionsH(r, ans, size, &x, &y);
// use x and y as you wish...
int total = x + 7 * y;

getPositionsH is given two pointers (the address) to x and y. Using *x = sets the value of x (declared before the function call).


Note

The

    *x = ans == 9 ? 0 : rand() % size;

statement is equivalent to

    if (ans == 9) *x = 0;
    else *x = rand() % size;
Déjà vu
  • 28,223
  • 6
  • 72
  • 100
0

return x; return y;

you have used return x. This function will return value to the calling function Now it will not come back and again return execute return y.Whenever you use return statement, you use keep in mind what actually return statement do ?

The return statement terminates the execution of a function and returns control to the calling function

So possibly you could refer this SO Q&A for solution of your problem. Other answers are also quite nice.

Community
  • 1
  • 1
Punit Vara
  • 3,744
  • 1
  • 16
  • 30