#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    How to make function return itself


    Hello,
    I have a code in C that get only positive numbers.
    I did that if number1 < 0, it will exit ( return; ) from the program.
    I don't want it will exit from the program, I want that the question (Please enter positive number)
    will ask again and again and again until the user will write positive number.
    I know the way return main(); but if my request to positive number is located at the middle of the program,
    I don't want it will go to start of program, I want it will go again the question. I mean only (Please enter positive number) line.

    Help is very appreciated.
    Thanks!
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    You simply need a loop.

    Code:
    int ur;
    
    do {
        scanf("%d", &ur);
    } while(ur != 0);
    It is not about "function returning itself"
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Originally Posted by rolory
    I know the way return main(); but if my request to positive number is located at the middle of the program,
    I don't want it will go to start of program, I want it will go again the question. I mean only (Please enter positive number) line.
    Who taught you that technique? We need to know so that he can be eliminate from the gene pool before he infects more minds with such complete and utter idiocy! We know he's out there, because we keep getting victims of his malicious deception! And now you are the latest of his victims. Please identify this monster so that we can try to stop him!

    Never call main()! main is the entry point of the program. It is only called once and that is only by the program's start-up code and never by your code. If anybody ever tells you to call main(), then turn around and run away from him as fast as you possibly can! And don't forget to report him to the authorities!

    You need to understand what you need to do. Your question itself tells me that you don't have any idea of that. What is "make function return itself" supposed to mean? I learned to program in 1978 and have been programming professionally in C since 1991. I have no idea what that's supposed to mean. Which tells me that you do not have a clear idea of it yourself.

    Never "return" to a function by calling it! Rather, you return from a function by using a return statement or by letting the flow of control run to the end of the function. If you do something as brain-dead as "returning" to a function by calling it, then you will inadvertently perform a recursive call, which will do things that you will not understand, except that it's not doing what you want it to do. For one thing, that new call uses an entirely different set of local variables than the one you want to return to. For another, you start from the beginning of the function instead of returning to where it had called you. Use the normal techniques for returning from a function, the ones that were designed into the language. If you feel that you must dream up something exotic to do something, then you are doing it wrong!

    What is it that you want to do? This is what I gather from your request:
    1. prompt the user to enter a positive number.
    2. Read in the user's input.
    3. If the number is not valid, then go back to Step #1. Otherwise, proceed on with the rest of the program.

    I would recommend also displaying a short error message when the user enters an invalid value, because otherwise he will think that the program is broken.

    What that describes is a loop. You could use a while loop or a do-while. I would recommend a do-while since you will want to run it at least once. Inside the loop, prompt the user for input and read the input and test it for being valid. If it's valid, then that will be the condition to exit the loop and you can proceed with the rest of the program. But if it's invalid, then you display the error message and you don't get out of the loop. You just stay in that loop, repeatedly prompting for input and reading input and testing that input, until the user finally gets it right. No function call, no "return", just a simple loop.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Disclose your program please. Our common language is c.
    Code:
    #include<stdio.h>
    
    int main() {
      int bad;
      double n;
      char s[30];
      do {
        puts("please enter positive number (of reasonable magnitude)");
        if (NULL == fgets(s,20,stdin)) {
          fputs("wow!  we're through.\n", stderr);
          return 1;
        }
        bad = (1 != sscanf(s, "%lg", &n)) || (n <= 0);
      } while (bad);
      puts("Compliance appreciated.");
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,114
    Rep Power
    1803
    Recursion is not iteration!

    Recursion has its uses, but in 24 years as a professional software engineer, I have only selected it as the most appropriate solution to anything on probably no more than 3 occasions.

    Your requirement is wholly unsuited to recursion. Don't be foolish. Recursing main() is a really bad idea, like a snake eating its own tail.

    How did anybody even get to know about recursion before learning about iteration?
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    237
    Rep Power
    29
    Recursion is not iteration!
    Recursion could be iteration in some functional languages, but definitely it is not about C
    :D
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    Post


    Originally Posted by dwise1_aol
    Who taught you that technique? We need to know so that he can be eliminate from the gene pool before he infects more minds with such complete and utter idiocy! We know he's out there, because we keep getting victims of his malicious deception! And now you are the latest of his victims. Please identify this monster so that we can try to stop him!

    Never call main()! main is the entry point of the program. It is only called once and that is only by the program's start-up code and never by your code. If anybody ever tells you to call main(), then turn around and run away from him as fast as you possibly can! And don't forget to report him to the authorities!

    You need to understand what you need to do. Your question itself tells me that you don't have any idea of that. What is "make function return itself" supposed to mean? I learned to program in 1978 and have been programming professionally in C since 1991. I have no idea what that's supposed to mean. Which tells me that you do not have a clear idea of it yourself.

    Never "return" to a function by calling it! Rather, you return from a function by using a return statement or by letting the flow of control run to the end of the function. If you do something as brain-dead as "returning" to a function by calling it, then you will inadvertently perform a recursive call, which will do things that you will not understand, except that it's not doing what you want it to do. For one thing, that new call uses an entirely different set of local variables than the one you want to return to. For another, you start from the beginning of the function instead of returning to where it had called you. Use the normal techniques for returning from a function, the ones that were designed into the language. If you feel that you must dream up something exotic to do something, then you are doing it wrong!

    What is it that you want to do? This is what I gather from your request:
    1. prompt the user to enter a positive number.
    2. Read in the user's input.
    3. If the number is not valid, then go back to Step #1. Otherwise, proceed on with the rest of the program.

    I would recommend also displaying a short error message when the user enters an invalid value, because otherwise he will think that the program is broken.

    What that describes is a loop. You could use a while loop or a do-while. I would recommend a do-while since you will want to run it at least once. Inside the loop, prompt the user for input and read the input and test it for being valid. If it's valid, then that will be the condition to exit the loop and you can proceed with the rest of the program. But if it's invalid, then you display the error message and you don't get out of the loop. You just stay in that loop, repeatedly prompting for input and reading input and testing that input, until the user finally gets it right. No function call, no "return", just a simple loop.
    Thank all,
    especially to you.
    so what I should to it's what rodiongork did.
    Code:
    int x;
    
    do {
        scanf("%d", &x);
    if(x>0)
    printf("Invalid value. Please enter negative number\n");
    } while(x>0);
    printf("The negative number is %d\n", x);
    *As I know, if the do-while condition doesn't apply, the follow sentence is the else of the loop.
    Is that correct?

    thanks!!!

IMN logo majestic logo threadwatch logo seochat tools logo