Can someone please explain the meaning of these return 1 vs return 0 vs break. I know 1 generally means true and 0 generally means false but how would they effect a return statement? Also when would you wanna use a return statement instead of a break statement and vice versa?

Recommended Answers

All 8 Replies

return x; will return the value x to the calling routine. If you called the function with rt = func(); rt will contain whatever x was. break; will immediately exit a loop even though the loop is not finished.

for (i=0; i<10; i++)
{
    --stuff--
    if (i == 7) break;
    --more stuff--
}

This loop will exit when i = 7. It won't run until i >= 10. And --more stuff-- will not be executed either.

You realy should google C functions and C control statements....You'll find the answers in these queries...

return x; will return the value x to the calling routine. If you called the function with rt = func(); rt will contain whatever x was. break; will immediately exit a loop even though the loop is not finished.

for (i=0; i<10; i++)
{
    --stuff--
    if (i == 7) break;
    --more stuff--
}

This loop will exit when i = 7. It won't run until i >= 10. And --more stuff-- will not be executed either.

When I put "return 1" in a few of my programs it stopped execution of the rest of my program.

You realy should google C functions and C control statements....You'll find the answers in these queries...

Nothing useful in relationship to my question. I checked the first 20 links with no luck.
http://www.google.com/search?q=c+programming+return+1&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
http://www.google.com/search?q=c+programming+return+statement&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

When I put "return 1" in a few of my programs it stopped execution of the rest of my program.

Returning from a generic function, or returning from main? Returning from main means your program is "done".

Returning from a generic function, or returning from main? Returning from main means your program is "done".

A couple of while and for loops.

A couple of while and for loops.

... in a generic function, or in main? Returning from main means your program is "done".

You did not answer Dave's question. Although we can surmise from your answer it's from main()

You did not answer Dave's question. Although we can surmise from your answer it's from main()

Could you please give me an example of returning from a function please?

ans = fnAdd(a, b);
printf("%d + %d = %d \n", a, b, ans);
int fnAdd(int x, int y)
{
    return (x+y);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.