Leegenux Blog

No man is an island

Modularization design of code has many advantages. As an essential role regarding to code modularization, exploitation of functions ought to be gotten the hang of.

The basic function declaration syntax is like this:

function funcIdentifier() {
# commands

or this

funcIdentifier () {
# commands

After the declaration, you can invoke the function like this

funcIdentifier param1 param2

And inside of the function, you can easily get the parameters using $1, $2, $3 ,etc.

But how to get the result value ? There are a few ways to do the trick.

  • Using echo inside of function and with the help of command substitution you get the result
  • eval command’ll be evaluated twice. First is of the contents at both side of equal sign, the second one is the actual value assignment
  • Global variables

In order to get stuffs across intuitively, I provide examples for first two approaches :

# 1.
function sum () {
echo `expr $1 + $2`

result=$(sum 12 13)

# 2.
function sum () {
eval $1='`' "expr $2 + $3" '`'
sum result 12 13

Understanding these two techniques of returning values from functions will help you understand more about the shell interpreter.

This article was last updated on days ago, and the information described in the article may have changed.