Functions

Introduction

Once programs start to get bigger than simple scripts, there is a need to organise them in order to make them more understandable and easier to maintain. This is where functions come in. Functions is a way to break down code and 'group' it together.

An example would be if you had a program that read a text file containing addresses and displayed it on screen in alphabetic order then wrote the sent it to a user via email. The program could be split up into the following functions

Read file

Display sorted file on screen

Send as email

As a rule of thumb, a function should only do one task. The second function 'Display sorted file on screen' could be broken down again in the following functions:

Sort file contents

Display on screen

The functions of the program would now be:

Read file

Sort file contents

Display on screen

Send as email

As can be seen, rather than one large program, it will be split up into a number of functions. The benefit of this is that it is easy to understand but also easy to maintain and debug. For example, if you wanted to an option to allow the user to decide whether the contents where to be sorted in normal or reverse order, this could be easily inserted into the program. Another example could be that there is a problem with emailing the file. With a monolithic program, you would have to go through the whole code to find the code that does with whereas with one split into functions all you need to do is find the function that deals with sending the email, inspect and amend it. As it will be a small piece of code, it should be easy to find the bug.

Structure

The structure of functions are as follows

function function_name (variable_list) {

  • function code

}

Where:

  • function_name is name of function variable_list is list of variables separated by commas. if there is none, this is

    • left blank

    function code is the code inside the function

If you want to return the contents of a variable that is used inside the function, the structure would be:

function function_name (variable_list) {

  • function code return (variable);

}

Please note the return statement returns variable. Only one variable can be sent back and it does not mean that it has to be one of the variables passed to it.

To call the function simply using the statement:

function_name (variable_list)

or the following if no variables are being passed to the function:

function_name ()

To receive a variable passed back from the function, use the following:

var variable = function_name ()

or

var variable = function_name (variable_list)

Example

A simple example of using a function is as follows:

   1 #!/usr/bin/env seed
   2 
   3 
   4 function hello_world() 
   5 {
   6   print ("Hello World!");
   7 }
   8 
   9 hello_world();

This simply calls the hello_world function which displays Hello World! onto the console

Variable Scope

As well as breaking down code in manageable chunks, it is necessary to make them more independent of each other. When variables are created in the main program, they are referred to as global variables as they are in the scope of the whole program. If these variables are passed to a function, they are referred to as local variables as they are in the scope of the function.

It may be asked why is this required? The reason being for debugging purposes. In a function, you may enter code that accidentally changes the contents of a variable causing the program to not work properly. If this happens, you would have to go through the whole program to find it which can be daunting if it is large. If on the other hand, if you passed the global variable to the function as a local variable, when the function finished, the variable reverts back to the global state's value. You should find it easier to find the bug as it will only happen when it runs the particular function.

As mention, the value of these variables reverts back once the function finishes. If you want the global variable to be changed by the function then you would specify in the function to return the local variables value.

The more information about variable scope, please click on the following wiki link:

http://en.wikipedia.org/wiki/Variable_%28programming%29#Scope_and_extent

Here is a example of using passing variables in functions.

   1 #!/usr/bin/env seed
   2 
   3 // Global variable
   4 var my_number = 5;
   5 
   6 function change_number(my_number) 
   7 {
   8   // my_number is now a local variable
   9   print ("my_number has been passed to function. It's value is " + my_number);
  10   my_number = 30;
  11   print ("function has changed value of my_number to " + my_number);
  12 }
  13 
  14 print ("contents of my_number before passing it to function is  " + my_number);
  15 change_number(my_number );
  16 print ("contents of my_number after passing it to function is  " + my_number);

As you can see from the comments, the global variable my_number is used in the function change_number. Even though they have the same name they are two totally different variables as witnessed by the following output:

contents of my_number before passing it to function is  5
my_number has been passed to function. It's value is 5
function has changed value of my_number to 30
contents of my_number after passing it to function is  5

If you wanted the variable my_number to be changed, then you would use the following code:

   1 #!/usr/bin/env seed
   2 
   3 function change_number(my_number) 
   4 {
   5   print ("my_number has been passed to function. It's value is " + my_number);
   6   my_number = 30;
   7   print ("function has changed value of my_number to " + my_number);
   8   return (my_number);
   9 }
  10 
  11 var my_number = 5;
  12 
  13 print ("contents of my_number before passing it to function is  " + my_number);
  14 my_number = change_number(my_number );
  15 print ("contents of my_number after passing it to function is  " + my_number);

The output being:

contents of my_number before passing it to function is  5
my_number has been passed to function. It's value is 5
function has changed value of my_number to 30
contents of my_number after passing it to function is  30

While it is perfectly acceptable to call the local variable that was passed to a function the same as the global variables name, in certain project may require conventions to be obeyed where the local and global variables need to have different names. An example of this would be:


function change_number(cn_my_number) 
{
  print ("my_number has been passed to function. It's value is " + cn_my_number);
  cn_my_number = 30;
  print ("function has changed value of my_number to " + cn_my_number);
  return (cn_my_number);
}

function multiply_number_only(mno_my_number, mno_second_number)
{
  mno_my_number *= mno_second_number;
  print ("my_number multiplied by second_number is " + mno_my_number); 
}


var my_number = 5;
var second_number = 4
print ("contents of my_number before passing it to function is  " + my_number);
my_number = change_number(my_number);
print ("contents of my_number after passing it to function is  " + my_number);

my_number = 5;
print ("contents of my_number has been changed back to " + my_number);

multiply_number_only(my_number, second_number);
print ("contents of my_number after passing it to function is  " + my_number);

The above convention prefixes the global variable my_number with the initials of function and underscore.

Projects/Seed/Tutorial/basic/functions (last edited 2013-11-22 19:19:54 by WilliamJonMcCann)