Home‎ > ‎CIS 98‎ > ‎

Understanding Functions

A characteristic of programming languages is the ability to factor out segments of common code that can be executed multiple times. Such code blocks are called functions, and they are stored in the shell's environment in the same way that variables are dynamically allocated and stored. The syntax for a shell function is:

  1. Objectives
    • Define the term function, and describe its syntax in a shell program.
    • Describe the situations in which a programmer should define and use functions.
    • Distinguish between a function definition and a function call.
    • Define a function library and describe how one is used in a shell script.
    • Describe the significance of the return status of an instruction or function
    • Use the return statement to exit a function with a specific
    • Describe the role of the logical operators || and && in controlling the flow of a program value.
  2. Definition
    A function is a collection of statements, (commands), that is given a name for purposes of clarifying the purpose of the statements and for repeated execution. 
    A function has two parts:
    • A name: function_name()
      • A name may be composed of letters, numbers, underscores and hyphens:
      • The name cannot begin with a digit.
    • A body: { statement(s) }
  3. Uses of Functions
    • Functions are used to define new commands that are made from a sequence of existing commands
    • Functions are used to factor out common blocks of code so that the code resides in one place rather than throughout the code.
    • Functions make programming code easier to follow and understand if the name of the function is appropriately chosen.
  4. Function Calls
    The code within a function's body is executed by "calling" the name of the function.
    	3a) turnright
    	3b) turnright
    	3c) turnright
    	3d) turnright
    1) move
    2) move
    3) twirl
    4) move
    5) stop
    In the above program, the line numbers are there for reference only.
    The order of execution of the above statements will be: 1, 2, 3a, 3b, 3c, 3d, 4, 5
    A function must be defined before it is called. Defining a function does not necesarily mean that the statements within it will ever be executed.

  5. Function Libraries
    • Functions may be placed in a file separate from the program.
      If a program wants to access the functions, it can do so with the source statement:
      source function-library
    • The shell will use it's PATH environment variable to locate the function-library if you do not give it a specific pathname. (This is important to know!)
    • After a function library is "sourced" any of the funcitons contained within it may be called by invoking the name of the function.
    • Question: When should functions be declared in libraries, and when should they be declared within a program?

  6. Return Codes

    When a command or function is finished executing, a return code (number between 0 and 255) is returned to the system. This number can be used to determine if the comamnd or function was successful in its execution.
    • You cannot change the return codes of commands, (they are usually described in man pages).
    • To specify what code your function should return, use the return statement.
    • Syntax:
      return [n]
    • If n is omitted, $? is assumed.
    • a 0 return code means success, a non-zero code means failure.

  7. Using Return Codes to Control FLow
    • Logical AND     (&&)
      command1 && command2
      command2 executes if and only if command1 executes successfully.
    • Logical OR     (||)
      command1 || command2
      command2 executes if and only if command1 executes unsuccessfully.
    • Logical NOT     (!)
      ! command
      inverts the return code from command: success becomes failure and vice versa.
  8. Shell scripts:
    1. Write a script that will test for three arguments. If those arguments are not present, prompt the user for those missing arguments.
    2. Write a shell script that will loop through the arguments on the command line and display them to the screen only if they are UNIX files.
    3. Write a shell script that will extract, sort and list all users' names on the screen.
    4. Write a script that tell syou whether the permissions for two files, whose names are given as arguments to the script are identical. If the permissions for the two files are identical, display the permission field; otherwise, displau each file name followed by its permissions.
    5. Write a script that takes the name of a directoru as an argument and searches the file heirarchy at that specific directory for any files that have zero lengths. Write the names of any zero length files to standard output.