Hello,

On 12/8/21 11:29, vani arul wrote:
Thanks for your help.
I am not good at programming.My code works perfectly.But I am bit confused about the return values.

Binary Search Program

/def Binarysearch(a,key):
    l=0
    r=len(a)-1
    while l<=r:
        med = (l+r)//2
        if key==a[med]:
            return med
        elif key>a[med]:
            l=med+1/

/      elif key<a[med]:
            r=med-1
    return -1
a=[6,8,23,33,44,55,66]
found=Binarysearch(a,66)
print("result:",found)"""/

 if key==a[med] condition is satisfied,the the block returns med.

But in the following block,how does it return values if there is no return specified?

First of all please reply always to the list address!

If Python does not encounter a return statement it will continue to run the function.

In your example the while loop will continue until the condition 'l<=r' is true. If Python exits the while loop then there is a return -1.

BR,

Roland





Regards
Vani

On Wed, Dec 8, 2021 at 2:15 AM Roland Mueller <roland.em0...@googlemail.com> wrote:

    Hello

    ti 7. jouluk. 2021 klo 19.47 vani arul (arulvan...@gmail.com)
    kirjoitti:

        Hey There,
        Can someone help to understand how a python function can
        return value with
        using return in the code?
        It is not not about explicit or implicit function call.


    Not sure whether I understood your question: I have a simple
    example about return.
      * f() and h() explicitely return something
      * g() and h() return None

    BTW, also Python documentation tool pydoc has an article about return.

    #!/usr/bin/python

    def f():
        return 42
    def g():
        pass
    def h():
        return

    if __name__ == "__main__":
        print(f"f(): {f()}")
        print(f"g(): {g()}")
        print(f"h(): {h()}")

    Result:
    f(): 42
    g(): None
    h(): None

    Pydoc:
    $ pydoc return

    BR,
    Roland


        Thanks
        Vani
-- https://mail.python.org/mailman/listinfo/python-list

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to