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.

2
  • Now its not returning anything?? Commented Dec 12, 2015 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. Commented Dec 12, 2015 at 13:52

4 Answers 4

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;
.....
Sign up to request clarification or add additional context in comments.

6 Comments

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.
@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?
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?
@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.
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.
|
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.

2 Comments

however it doesnt even return x... im not sure thats the problem
@chris: It's certainly a problem. That doesn't mean you don't have other problems too.
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;

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.