Linux and Networking

Linux and Networking

4.7. Returning to functions

We already saw in a previous post the fundamentals of functions. The functions will not only help us make the programs more organized, but also to reuse the code or even do recursion (although with shell script it is not your thing).

Let’s see examples of a somewhat more advanced use of functions:

I don’t know if you noticed in the previous exercise, but TATACHAN will never appear on the screen. If you notice, the function skip causes the script to terminate right then and there.
A fundamental characteristic of functions are the arguments. The arguments are used to parameterize the functions and make them more generic.
Here is an example of functions with arguments:

If you notice, the show function takes a parameter that is $1. If you wanted to pass two parameters, they would be $1 for the first and $2 for the second (and so on). This function currently only displays the parameter on the screen, but you could do many more things with it.

Suggested exercise:
Create a script with a factorial function that displays the factorial of the number passed as a parameter on the screen. Don’t use recursion, use the loops seen before.

The Demeter Project