www.webdeveloper.com
Results 1 to 9 of 9

Thread: A question about return

  1. #1
    Join Date
    Jul 2012
    Posts
    29

    A question about return

    Hello,

    I wanted to write a program which takes an input. If the input is an integer, it prints it, otherwise it will ask again and again.

    This is the solution I could think about:

    PHP Code:
    <?php

    printf
    ("I'd like an integer please: ");
    take_number();

    function 
    take_number() {    
        
    fscanf(STDIN"%d\n"$number);
        if (
    is_int($number)) {
            
    printf("%d"$number);
        } else {
            
    printf("I'd like an integer please: ");
            
    take_number();
        }
    }

    exit(
    0);
    ?>
    Output:

    Code:
    I'd like an integer please: opolmkk
    I'd like an integer please: ol
    I'd like an integer please: rt
    I'd like an integer please: t
    I'd like an integer please: M
    I'd like an integer please: 457
    457
    But my question is why does the following fail?

    PHP Code:
    <?php

    printf
    ("I'd like an integer please: ");
    printf("%d"take_number());

    function 
    take_number() {    
        
    fscanf(STDIN"%d\n"$number); 
        if (
    is_int($number)) {
            return 
    $number;
        } else {
            
    printf("I'd like an integer please: ");
            
    take_number();
        }
    }

    exit(
    0);
    ?>
    Output:
    Code:
    I'd like an integer please: o
    I'd like an integer please: l
    I'd like an integer please: p
    I'd like an integer please: J
    I'd like an integer please: 122
    0
    It returns a 0 except if your first input is an integer (or numeric).

    So, two thing which don't do the right job is_int() and return.

    is_int() problem:
    Output:
    Code:
    I'd like an integer please: 4.45
    4
    Thank you!

    Best regards,
    Nils

  2. #2
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    I don't use command line PHP, so I replicated your code to try it out for the browser.
    PHP Code:
    take_number();

    function 
    take_number() 
    {    
        
    $number NULL;
        
    $handle fopen("newfile.txt""r");

        while(!
    is_int($number)) 
        { 
             
    printf("I'd like an integer please: <br />");
            
    fscanf($handle"%d\n"$number);
        }  
        
    printf("%d"$number);

    Your input:
    Code:
    o
    l
    p
    j
    122
    Returns true once the 122 is found. No clue at all how this is working differently for you on the command line.

    Your code is returning 0 (false) when you enter 4.45 because that's not an integer, it's a floating point number. Integers are only whole numbers.

    To input floating point numbers, you need to use %f instead of %d. However, you can't input them both in your code because you're specifically asking for integers and are checking for them with is_int().

    You could input your data as a string with %s, but you'd need to use the address-of operator with that, and if you haven't learnt much about PHP, memory addresses, memory allocation, arrays or pointers (references), I wouldn't worry.

    Anyway, I think this should work for you:

    PHP Code:
    take_number();

    function 
    take_number() 
    {    
        
    $number NULL;

        while(!
    is_int($number)) 
        { 
             
    printf("I'd like an integer please: ");
            
    fscanf(STDIN"%d\n"$number);
        }  
        
    printf("%d"$number);

    Try to use the while-loop format for things like this, as it guarantees the code doesn't get stuck in an infinite loop and will always pause the loop between data input. You're code is using a recursive function call (calling a function within itself) and has 2 calls to the same function. Not advisable for this and unnecessary.

    I hope the code I gave you works, but as I said I don't use the command line so it may very well be different.

  3. #3
    Join Date
    Jul 2012
    Posts
    29

    Smile

    George88,
    Very nice. Thanks.
    Yes the last code you posted works but this is not the issue here because also the first code I put in my first post works fine.

    The question is why doesn't it work when I use return?
    PHP Code:
    if (is_int($number)) {
            return 
    $number;
        } 
    The second question: why does the program stop when I enter a numerical value which is not an integer?
    Code:
    I'd like an integer please: 3.78
    3

    P.S:
    Some people would argue that it's not a good practice to let a function call itself. If this is the case, then while has to be better. Anyway, I am trying to understand how functions work and how to use return and predefined functions as is_int() in this example.


    Kr,
    Nils

  4. #4
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    It's not bad practice to use recursive function calls, but they have a time and a place. This is not that time.

    As for your question regarding the program stopping when you enter something like 3.38; I already explained that. Your code is expecting an integer value. The lines:

    PHP Code:
    printf("I'd like an integer please: ");
    fscanf(STDIN"%d\n"$number); 
    Will only ever allow integers to be input. Your floating point numbers are simply being truncated by the function because of %d (decimal). To input floating point numbers (like 3.43), you need to use %f, or they will simply be truncated and treated as integers.

    Regarding the question about using return, you're using it incorrectly here. In order to use the return statement, you first need to have something which can store that return value, like a variable. Return simply passes back the desired value to the thing that called the function, so this would make sense:

    PHP Code:
    // Return value gets stored in here
    $return_int_to_this take_number();

    function 
    take_number() 
    {    
        
    $number NULL;

        while(!
    is_int($number)) 
        { 
            
    printf("I'd like an integer please: ");
            
    fscanf(STDIN"%d\n"$number);
        }  
        
    printf("%d"$number);
        
        
    // Return the value of $number to the variable.
        
    return $number;


  5. #5
    Join Date
    Jul 2012
    Posts
    29

    Thumbs up

    Just great George88.
    Thank you.

    I understand now why netbeans complains (returns a warning) about an uninitialized $number in fscanf(STDIN, "%d\n", $number);.

    I read PHP doesn't work like C for example in working with variables, define them and initialize them. So, I didn't do the $number = NULL.

    But the program I want to write is a one which only stops when you enter an Integer.

    PHP Code:
    while(!is_int($number)) 
        { 
            
    printf("I'd like an integer please: "); 
            
    fscanf(STDIN"%d\n"$number); 
        } 
    This loop should not stop even when I enter a floating number.

    This is an exercice for me.

    Kind regards,
    Nils

  6. #6
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    Try using fgets(), because your scanf() function is always converting your input to integer with &#37;d.

    If you put your fgets() function in the while loop condition, you can check the values of the variable in the body of the loop and use break; if your conditions are met. I won't give you the answer because I don't actually know it, but something like:

    PHP Code:
    $number NULL;
    while(
    $number fgets(params_here))
    {
        
    // etc.

    Also, you should probably get in to the habit of initialising your variables. I'm not entirely sure how PHP handles uninitialised variables, but in C/C++, any uninitialised variables will contain random "junk" characters because of past items allocated to that address, or something along those lines anyway.

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Nils28 View Post
    PHP Code:
    while(!is_int($number)) 
        { 
            
    printf("I'd like an integer please: "); 
            
    fscanf(STDIN"%d\n"$number); 
        } 
    This loop should not stop even when I enter a floating number.
    You are formatting the input to an integer if there are any digits within the string. That is why it is stopping when you input a float/double. fgets would probably be a better solution ($line = trim(fgets(STDIN)); ). echo is also a better choice than printf since you are not looking to format using an expression and are solely putting output on the screen.

  8. #8
    Join Date
    Jul 2012
    Posts
    29
    No, fgets() doesn't work, but never mind.
    Thanks for the solution on how to use return. (This works).

    But it is a crazy solution done in PHP and that is one of the dozes of reasons why I started to feel not comfortable to continue learning this language.


  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Nils28 View Post
    No, fgets() doesn't work, but never mind.
    Thanks for the solution on how to use return. (This works).

    But it is a crazy solution done in PHP and that is one of the dozes of reasons why I started to feel not comfortable to continue learning this language.

    Did you get any errors when trying to use fgets?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles