Let’s take a quick look at how a “function” is defined in a few different scripting languages. Why? Why not?

desk_spill_large

What is a Function

Borrowed from the mathematics world, as was pretty much everything computer-related, the term “function” (as a noun) refers to a distinct entity which performs a task and may or may not return a result.

The developer’s text book examples usually involve a simple “AddNumbers” function.  The basic form expects you to provide two or more numeric values and it will add them and return the sum total.  In pseudo-code, it might look like this…

total_count = AddNumbers (2, 4)

You can guess that total_count would be assigned the value 6 when completed.  In any case, you get the general idea.  Now, let’s look at what the function itself looks like in a few different languages.

For each language, there’s both a 2-argument form, and a variable-length argument form.  In other words, the first example expects only 2 numbers, while the second option allows for 1, 2, 3 or any number of numbers to add up.  The first option expects numeric values for the two arguments, but the second requires an Array or List (depending on the language).

total_count = AddNumbers (2, 4)

total_count = AddNumbers (1,3,5,6.2,0.55)

To implement the second form, you have to define the function to use an Array or List data type as a single argument.  This is sort of like putting the values into a box, passing the box to the function and the function opens the box to process the contents.  In most respects, the latter form (variable number or array argument) is the most flexible.

PowerShell

function AddNumbers {
  Param(
    [parameter(Mandatory=$True)]$FirstNumber,
    [parameter(Mandatory=$True)]$SecondNumber
  )
  write-output $FirstNumber + $SecondNumber
}

# variable number of arguments...

function AddNumbers {
  Param(
    [parameter(Mandatory=$True)]$NumbersList
  )
  $result = 0
  foreach ($num in $NumbersList) {
    $result += $num
  }
  write-output $result
}

PHP

function AddNumbers ($number1, $number2) {
  return $number1 + $number2;
}

// variable number of arguments...

$NumList = array(1,2,3,4,5);

function AddNumbers ($array) {
  return array_sum($array);
}

JavaScript

function AddNumbers (FirstNumber, SecondNumber) {
  return FirstNumber + SecondNumber;
}

// variable number of arguments...

function AddNumbers (NumbersList) {
  var result = 0;
  for (i = 0; i < NumbersList.length; i++) {
    result += 0;
  }
  return result;
}

VBscript

function AddNumbers (FirstNumber, SecondNumber)
  AddNumbers = FirstNumber + SecondNumber
end function

' variable number of arguments...

function AddNumbers (NumList)
  dim result : result = 0
  foreach num in NumList
    result = result + num
  next
  AddNumbers = result
end function

Visual LISP

(defun AddNumbers (FirstNumber, SecondNumber)
  (+ FirstNumber SecondNumber)
)

' variable number of arguments...

(defun AddNumbers (NumList)
  (apply '+ NumList)
)

I hope you found this to be helpful and useful in some way.  Cheers!

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s