InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
Bash
Using Functions: Parameters and Return Values in bash
Bash

Using Functions: Parameters and Return Values in bash

InfinityCoder February 20, 2017

You want to use a function and you need to get some values into the function. How do you pass in parameters? How do you get values back?

You don’t put parentheses around the arguments like you might expect from some programming languages.

Put any parameters for a bash function right after the function’s name, separated by whitespace, just like you were invoking any shell script or command.

Don’t forget to quote them if necessary!

1
2
3
4
5
6
7
8
# define the function:
function max ( )
{ ... }
#
# call the function:
#
max 128 $SIM
max $VAR $CNT

You have two ways to get values back from a function. You can assign values to variables inside the body of your function.

Those variables will be global to the whole script unless they are explicitly declared local within the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# cookbook filename: func_max.1
 
# define the function:
function max ( )
{
   local HIDN
   if [ $1 -gt $2 ]
   then
      BIGR=$1
   else
      BIGR=$2
   fi
   HIDN=5
}

For example:

1
2
3
4
# call the function:
max   128   $SIM
# use the result:
echo $BIGR

The other way is to use echo or printf to send output to standard output.

Then you must invoke the function inside a $( ), capturing the output and using the result, or it will be wasted on the screen:

1
2
3
4
5
6
7
8
9
10
11
12
# cookbook filename: func_max.2
 
# define the function:
function max ( )
{
   if [ $1 -gt $2 ]
   then
      echo $1
   else
      echo $2
   fi
}

For example:

1
2
3
4
# call the function:
BIGR=$(max    128  $SIM)
# use the result
echo $BIGR

Putting parameters on the invocation of the function is just like calling any shell script.

The parameters are just the other words on the command line.
Within the function, the parameters are referred to as if they were command-line arguments by using $1, $2, etc. However, $0 is left alone.

It remains the name by which the entire script was invoked. On return from the function, $1, $2, etc. are back to referring to the parameters with which the script was invoked.
Also of interest is the $FUNCNAME array. $FUNCNAME all by itself references the zeroth element of the array, which is the name of the currently executing function.

In other words, $FUNCNAME is to a function as $0 is to a script, except without all the path information.

The rest of the array elements is hat amounts to a call stack, with “main” as the bottom or last element.

This variable only exists while a function is executing.

We included the useless variable $HIDN just to show that it is local to the function definition.

Even though we can assign it values inside the function, any such value would not be available elsewhere in the script.

It is a variable whose value is local to that function; it comes into existence when the function is called, and is gone once the function returns.

Returning values by setting variables is more efficient, and can handle lots of data— many variables can be set—but the approach has its drawbacks.

It requires that the function and the rest of the script agree on variable names for the information handoff.
This kind of coupling has maintenance issues.

The other approach, using the output as the way to return values, does reduce this coupling, but is limited in its usefulness—it is limited in how much data it can return before your script has to spend lots of effort parsing the results of the function.

So which to use? As with much of engineering, this, too, is a trade-off and you have to decide based on your specific needs.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Consuming Arguments in bash
For any serious shell script, you are likely to have …

Consuming Arguments in bash

Setting bash As Your Default Shell in bash
You’re using a BSD system, Solaris, or some other Unix …

Setting bash As Your Default Shell in bash

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap