Returning Values from Functions

Tutorial 3 of 5

1. Introduction

In this tutorial, we will be focusing on how to return values from Shell Functions. Functions in Shell are a way of packaging code that you will use repeatedly. You can save time by calling these functions instead of writing the same code multiple times. The main goal of this tutorial is to understand how to capture results from a function and use them in other parts of our script.

By the end of this tutorial, you will be able to:

  • Understand the concept of returning values from functions in Shell
  • Write your own functions that return values
  • Use returned values in your script

Prerequisites:
Basic knowledge of Shell scripting is required to follow along with this tutorial.

2. Step-by-Step Guide

In Shell, functions do not directly return values like other programming languages. Instead, they output the result, which can then be captured in a variable.

Let's first understand how to create a simple function in Shell. A Shell function is defined like this:

function_name () {
  # code goes here
}

To return a value from a function, we use the command echo. The echo command is used to print data to the standard output, which we can then capture in a variable.

Here's an example:

get_name () {
  echo "John Doe"
}

name=$(get_name)
echo $name  # This will print: John Doe

In this example, the get_name function outputs "John Doe", which is then captured in the name variable.

3. Code Examples

Let's look at a few more practical examples:

Example 1: Function that returns the sum of two numbers

# Function definition
sum () {
  local num1=$1  # The first argument to the function
  local num2=$2  # The second argument to the function
  echo $((num1 + num2))  # The sum of the two numbers
}

# Using the function
result=$(sum 5 3)
echo $result  # This will print: 8

Example 2: Function that checks if a number is even or odd

# Function definition
is_even () {
  local num=$1
  if (( num % 2 == 0 )); then
    echo "even"
  else
    echo "odd"
  fi
}

# Using the function
result=$(is_even 7)
echo $result  # This will print: odd

4. Summary

In this tutorial, we have covered how to return values from Shell functions. We learned that functions in Shell output the result, which can then be captured in a variable using the echo command. We also looked at several examples of functions that return values.

For further learning, you can explore how to use functions with different types of arguments and how to handle errors in functions.

5. Practice Exercises

  1. Write a function that takes a string as an argument and returns the length of the string.
  2. Write a function that takes two numbers as arguments and returns the bigger number.

Solutions

length_of_string () {
  local str=$1
  echo ${#str}
}

result=$(length_of_string "Hello, World!")
echo $result  # This will print: 13
bigger_number () {
  local num1=$1
  local num2=$2
  if (( num1 > num2 )); then
    echo $num1
  else
    echo $num2
  fi
}

result=$(bigger_number 5 9)
echo $result  # This will print: 9

Remember, practice is key to improving your programming skills. Try to come up with your own examples and use cases for functions that return values.