Is `return` within function a compulsory requirement?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
3
down vote

favorite












The the simpler form of a function is:



name () commands return 


I find no difference in a function with and without return.

Suppose the minimal code:



step_forward ()
echo "step one;"
return

turn_around()
echo "turn around."
return


step_forward
turn_around


Run and check the exit status:



$ bash testing.sh
step one;
turn around.
$ echo $?
0


Run it again after commenting out return



$ bash testing.sh
step one;
turn around.
$ echo $?
0


In what circumstances should a function end with a return?










share|improve this question



















  • 4




    return is not required. Normally you would use it to return an exit value like 1 or 0 based on a test, but with your script you are not returning an exit value but only echoing out something, return is not needed at all. A test is like if [[ $value == "true" ]]; then return 0; else return 1; fi
    – Terrance
    Apr 14 at 5:02











  • name () commands return ... why do you have return there? Who told you that's a simpler form?
    – muru
    Apr 14 at 6:42














up vote
3
down vote

favorite












The the simpler form of a function is:



name () commands return 


I find no difference in a function with and without return.

Suppose the minimal code:



step_forward ()
echo "step one;"
return

turn_around()
echo "turn around."
return


step_forward
turn_around


Run and check the exit status:



$ bash testing.sh
step one;
turn around.
$ echo $?
0


Run it again after commenting out return



$ bash testing.sh
step one;
turn around.
$ echo $?
0


In what circumstances should a function end with a return?










share|improve this question



















  • 4




    return is not required. Normally you would use it to return an exit value like 1 or 0 based on a test, but with your script you are not returning an exit value but only echoing out something, return is not needed at all. A test is like if [[ $value == "true" ]]; then return 0; else return 1; fi
    – Terrance
    Apr 14 at 5:02











  • name () commands return ... why do you have return there? Who told you that's a simpler form?
    – muru
    Apr 14 at 6:42












up vote
3
down vote

favorite









up vote
3
down vote

favorite











The the simpler form of a function is:



name () commands return 


I find no difference in a function with and without return.

Suppose the minimal code:



step_forward ()
echo "step one;"
return

turn_around()
echo "turn around."
return


step_forward
turn_around


Run and check the exit status:



$ bash testing.sh
step one;
turn around.
$ echo $?
0


Run it again after commenting out return



$ bash testing.sh
step one;
turn around.
$ echo $?
0


In what circumstances should a function end with a return?










share|improve this question















The the simpler form of a function is:



name () commands return 


I find no difference in a function with and without return.

Suppose the minimal code:



step_forward ()
echo "step one;"
return

turn_around()
echo "turn around."
return


step_forward
turn_around


Run and check the exit status:



$ bash testing.sh
step one;
turn around.
$ echo $?
0


Run it again after commenting out return



$ bash testing.sh
step one;
turn around.
$ echo $?
0


In what circumstances should a function end with a return?







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 14 at 8:25









Zanna

48k13119228




48k13119228










asked Apr 14 at 4:58









JawSaw

395111




395111







  • 4




    return is not required. Normally you would use it to return an exit value like 1 or 0 based on a test, but with your script you are not returning an exit value but only echoing out something, return is not needed at all. A test is like if [[ $value == "true" ]]; then return 0; else return 1; fi
    – Terrance
    Apr 14 at 5:02











  • name () commands return ... why do you have return there? Who told you that's a simpler form?
    – muru
    Apr 14 at 6:42












  • 4




    return is not required. Normally you would use it to return an exit value like 1 or 0 based on a test, but with your script you are not returning an exit value but only echoing out something, return is not needed at all. A test is like if [[ $value == "true" ]]; then return 0; else return 1; fi
    – Terrance
    Apr 14 at 5:02











  • name () commands return ... why do you have return there? Who told you that's a simpler form?
    – muru
    Apr 14 at 6:42







4




4




return is not required. Normally you would use it to return an exit value like 1 or 0 based on a test, but with your script you are not returning an exit value but only echoing out something, return is not needed at all. A test is like if [[ $value == "true" ]]; then return 0; else return 1; fi
– Terrance
Apr 14 at 5:02





return is not required. Normally you would use it to return an exit value like 1 or 0 based on a test, but with your script you are not returning an exit value but only echoing out something, return is not needed at all. A test is like if [[ $value == "true" ]]; then return 0; else return 1; fi
– Terrance
Apr 14 at 5:02













name () commands return ... why do you have return there? Who told you that's a simpler form?
– muru
Apr 14 at 6:42




name () commands return ... why do you have return there? Who told you that's a simpler form?
– muru
Apr 14 at 6:42










1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










A return value is not required in a function. Normally a return would be used in a script for an exit value to be returned. Exit values are normally like a 1 or a 0 where a lot of scripters might use it for a 0 as successful and a 1 as not successful.



#!/bin/bash
#The following function returns a value of 0 or 1
function if_running() grep -v grep > /dev/null
if [[ $? == 0 ]]; then
return 0
else
return 1
fi


#Read in name of a running process
read -p "Enter a name of a process: "

#Send REPLY to function
if_running $REPLY

#Check return value and echo appropriately
if [[ $? == 0 ]]; then
echo "Return value is $?"
echo "$REPLY is running..."
else
echo "Return value is $?"
echo "$REPLY is not running..."
fi


Examples:



~$ ./ps_test.bsh 
Enter a name of a process: ls
Return value is 1
ls is not running...

~$ ./ps_test.bsh
Enter a name of a process: bash
Return value is 0
bash is running...


And this answer I wrote a little bit ago does not have return values but still gives output https://askubuntu.com/a/1023493/231142



#!/bin/bash
function area()
circ=$(echo "3.14 * $1^2"

#Read in radius
read -p "Enter a radius: "

#Send REPLY to function
area $REPLY

#Print output
echo "Area of a circle is $circ"


Example:



terrance@terrance-ubuntu:~$ ./circ.bsh 
Enter a radius: 6
Area of a circle is 113.04


Hope this helps!






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1024856%2fis-return-within-function-a-compulsory-requirement%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    4
    down vote



    accepted










    A return value is not required in a function. Normally a return would be used in a script for an exit value to be returned. Exit values are normally like a 1 or a 0 where a lot of scripters might use it for a 0 as successful and a 1 as not successful.



    #!/bin/bash
    #The following function returns a value of 0 or 1
    function if_running() grep -v grep > /dev/null
    if [[ $? == 0 ]]; then
    return 0
    else
    return 1
    fi


    #Read in name of a running process
    read -p "Enter a name of a process: "

    #Send REPLY to function
    if_running $REPLY

    #Check return value and echo appropriately
    if [[ $? == 0 ]]; then
    echo "Return value is $?"
    echo "$REPLY is running..."
    else
    echo "Return value is $?"
    echo "$REPLY is not running..."
    fi


    Examples:



    ~$ ./ps_test.bsh 
    Enter a name of a process: ls
    Return value is 1
    ls is not running...

    ~$ ./ps_test.bsh
    Enter a name of a process: bash
    Return value is 0
    bash is running...


    And this answer I wrote a little bit ago does not have return values but still gives output https://askubuntu.com/a/1023493/231142



    #!/bin/bash
    function area()
    circ=$(echo "3.14 * $1^2"

    #Read in radius
    read -p "Enter a radius: "

    #Send REPLY to function
    area $REPLY

    #Print output
    echo "Area of a circle is $circ"


    Example:



    terrance@terrance-ubuntu:~$ ./circ.bsh 
    Enter a radius: 6
    Area of a circle is 113.04


    Hope this helps!






    share|improve this answer


























      up vote
      4
      down vote



      accepted










      A return value is not required in a function. Normally a return would be used in a script for an exit value to be returned. Exit values are normally like a 1 or a 0 where a lot of scripters might use it for a 0 as successful and a 1 as not successful.



      #!/bin/bash
      #The following function returns a value of 0 or 1
      function if_running() grep -v grep > /dev/null
      if [[ $? == 0 ]]; then
      return 0
      else
      return 1
      fi


      #Read in name of a running process
      read -p "Enter a name of a process: "

      #Send REPLY to function
      if_running $REPLY

      #Check return value and echo appropriately
      if [[ $? == 0 ]]; then
      echo "Return value is $?"
      echo "$REPLY is running..."
      else
      echo "Return value is $?"
      echo "$REPLY is not running..."
      fi


      Examples:



      ~$ ./ps_test.bsh 
      Enter a name of a process: ls
      Return value is 1
      ls is not running...

      ~$ ./ps_test.bsh
      Enter a name of a process: bash
      Return value is 0
      bash is running...


      And this answer I wrote a little bit ago does not have return values but still gives output https://askubuntu.com/a/1023493/231142



      #!/bin/bash
      function area()
      circ=$(echo "3.14 * $1^2"

      #Read in radius
      read -p "Enter a radius: "

      #Send REPLY to function
      area $REPLY

      #Print output
      echo "Area of a circle is $circ"


      Example:



      terrance@terrance-ubuntu:~$ ./circ.bsh 
      Enter a radius: 6
      Area of a circle is 113.04


      Hope this helps!






      share|improve this answer
























        up vote
        4
        down vote



        accepted







        up vote
        4
        down vote



        accepted






        A return value is not required in a function. Normally a return would be used in a script for an exit value to be returned. Exit values are normally like a 1 or a 0 where a lot of scripters might use it for a 0 as successful and a 1 as not successful.



        #!/bin/bash
        #The following function returns a value of 0 or 1
        function if_running() grep -v grep > /dev/null
        if [[ $? == 0 ]]; then
        return 0
        else
        return 1
        fi


        #Read in name of a running process
        read -p "Enter a name of a process: "

        #Send REPLY to function
        if_running $REPLY

        #Check return value and echo appropriately
        if [[ $? == 0 ]]; then
        echo "Return value is $?"
        echo "$REPLY is running..."
        else
        echo "Return value is $?"
        echo "$REPLY is not running..."
        fi


        Examples:



        ~$ ./ps_test.bsh 
        Enter a name of a process: ls
        Return value is 1
        ls is not running...

        ~$ ./ps_test.bsh
        Enter a name of a process: bash
        Return value is 0
        bash is running...


        And this answer I wrote a little bit ago does not have return values but still gives output https://askubuntu.com/a/1023493/231142



        #!/bin/bash
        function area()
        circ=$(echo "3.14 * $1^2"

        #Read in radius
        read -p "Enter a radius: "

        #Send REPLY to function
        area $REPLY

        #Print output
        echo "Area of a circle is $circ"


        Example:



        terrance@terrance-ubuntu:~$ ./circ.bsh 
        Enter a radius: 6
        Area of a circle is 113.04


        Hope this helps!






        share|improve this answer














        A return value is not required in a function. Normally a return would be used in a script for an exit value to be returned. Exit values are normally like a 1 or a 0 where a lot of scripters might use it for a 0 as successful and a 1 as not successful.



        #!/bin/bash
        #The following function returns a value of 0 or 1
        function if_running() grep -v grep > /dev/null
        if [[ $? == 0 ]]; then
        return 0
        else
        return 1
        fi


        #Read in name of a running process
        read -p "Enter a name of a process: "

        #Send REPLY to function
        if_running $REPLY

        #Check return value and echo appropriately
        if [[ $? == 0 ]]; then
        echo "Return value is $?"
        echo "$REPLY is running..."
        else
        echo "Return value is $?"
        echo "$REPLY is not running..."
        fi


        Examples:



        ~$ ./ps_test.bsh 
        Enter a name of a process: ls
        Return value is 1
        ls is not running...

        ~$ ./ps_test.bsh
        Enter a name of a process: bash
        Return value is 0
        bash is running...


        And this answer I wrote a little bit ago does not have return values but still gives output https://askubuntu.com/a/1023493/231142



        #!/bin/bash
        function area()
        circ=$(echo "3.14 * $1^2"

        #Read in radius
        read -p "Enter a radius: "

        #Send REPLY to function
        area $REPLY

        #Print output
        echo "Area of a circle is $circ"


        Example:



        terrance@terrance-ubuntu:~$ ./circ.bsh 
        Enter a radius: 6
        Area of a circle is 113.04


        Hope this helps!







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 14 at 14:39

























        answered Apr 14 at 5:28









        Terrance

        17.3k23784




        17.3k23784



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1024856%2fis-return-within-function-a-compulsory-requirement%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Trouble downloading packages list due to a “Hash sum mismatch” error

            How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

            Which professions warranted travel in Medieval times?