Is `return` within function a compulsory requirement?

Clash 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?
bash
add a comment |Â
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?
bash
4
returnis 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,returnis not needed at all. A test is likeif [[ $value == "true" ]]; then return 0; else return 1; fi
â Terrance
Apr 14 at 5:02
name () commands return... why do you havereturnthere? Who told you that's a simpler form?
â muru
Apr 14 at 6:42
add a comment |Â
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?
bash
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
bash
edited Apr 14 at 8:25
Zanna
48k13119228
48k13119228
asked Apr 14 at 4:58
JawSaw
395111
395111
4
returnis 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,returnis not needed at all. A test is likeif [[ $value == "true" ]]; then return 0; else return 1; fi
â Terrance
Apr 14 at 5:02
name () commands return... why do you havereturnthere? Who told you that's a simpler form?
â muru
Apr 14 at 6:42
add a comment |Â
4
returnis 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,returnis not needed at all. A test is likeif [[ $value == "true" ]]; then return 0; else return 1; fi
â Terrance
Apr 14 at 5:02
name () commands return... why do you havereturnthere? 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
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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!
add a comment |Â
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!
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!
edited Apr 14 at 14:39
answered Apr 14 at 5:28
Terrance
17.3k23784
17.3k23784
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
4
returnis 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,returnis not needed at all. A test is likeif [[ $value == "true" ]]; then return 0; else return 1; fiâ Terrance
Apr 14 at 5:02
name () commands return... why do you havereturnthere? Who told you that's a simpler form?â muru
Apr 14 at 6:42