1. Arguments, return values, and scope
Let's learn how to get data into and out of our functions and how scope affects this process.
2. Passing arguments into Bash functions
Passing arguments into functions is just like using ARGV into your Bash script. The dollar-sign notation.
So, you have access to the special ARGV properties which were:
Using dollar-one, dollar-two etc. for specific ARGV elements, dollar-at for all elements and dollar-pound for the number of elements.
3. Passing arguments example
In this example, we will pass in some file names, loop through and print them out.
Inside our function code, we firstly echo the first ARGV element using dollar-one notation.
Then we use a FOR loop over all the elements using dollar-at.
The last line calls this function with some file names.
See how the output is the first file name and then each file name (including the first) in turn.
4. Scope in programming
Scope is an important programming concept. It refers to how accessible a variable is within the program.
Global scope is where a variable is accessible anywhere including inside sections such as FOR loops, IF statements, and functions.
Local scope is a variable only accessible in a certain part of the program.
Why is this important? Because if you try and access a variable outside where it has local scope - your program may fail with an error!
5. Scope in Bash functions
Bash makes it easy as all variables are global by default.
In this example function, we create a variable from the first argument inside.
After calling the function, we echo the variable created inside.
It works!
Beware. Many languages limit global scope by default as this carries risks you may accidentally do something unintended.
6. Restricting scope in Bash functions
To restrict a variable's scope, use the 'local' command.
Here we repeat the same function but restrict the variable 'first_filename' to be local inside the function.
We again call the function and try to print the variable created inside.
A blank line is printed.
But why a blank line and and not an error?
The answer is that 'first_filename' was assigned to the global first ARGV element (dollar-one).
The script was run with no arguments, just 'bash script.sh' so the first ARGV was blank. Hence, be careful considering scope!
7. Return values
Knowing how to get arguments in functions, how do we get data out?
'return' in Bash is not for data. It contains a value based on whether the function was successful (0) or not (other values 1-255). This value is in the global variable dollar-question-mark.
Therefore, we could:
Assign what we want to a global variable and access anywhere outside. As we just did in this video.
Or, we can echo what we want returned as the last line in the function and capture using shell-within-a-shell.
8. A return error
This is an example of return being used with an error.
We create a function that has an error inside.
We call the function then print out the return value using dollar-question-mark
This is the output.
So what happened?
As expected, trying to call a non-existent program 'echlo' did not work and there was an error.
The return value 127 means the script tried to find a program that doesn't exist.
9. Returning correctly
Let's return what we want using the second method. 'echo' and shell-within-a-shell.
Using the temperature conversion function from before, we echo back the calculation result inside the function.
Below, we assign the variable 'converted' to what is returned from the function when using the argument '30'. Then echo using that variable.
We get the correct return value.
Notice how we didn't use an intermediary variable this time?
10. Let's practice!
Let's practice moving data in and out of Bash functions using arguments and return values!