logo for matrixlab-examples.com
leftimage for matrixlab-examples.com


Programming Functions – Basic Concepts

Before looking at different examples of programming functions, it’s better to understand the purpose and definition of function. 
 


A function in programming is the means by which someone who designs or uses a program can execute a block of code which has two purposes: to finish a certain routine or task and/or to return processed values.

A function is also considered as a procedure in some programming languages. 

On the other hand, software functions are commonly known as any term that is being used in referring to names of code blocks. Functions accept restrictions, they also return values, and they usually are maintained on a separate location from the code of the main program. 

Perhaps, the most common example of a programming function is a mathematical function.  ‘log’ (for logartithm) and ‘tan’ (for tangent) are examples of mathematical functions.  There are other functions to work with ‘strings’ (alphanumerical characters) and time functions, just to name a few...

Simply expressed, a programming function allows you to assign certain values where results can be calculated in a matter of seconds 

(usally much less) while saving you from the task of doing the operations or computations manually. 

On the declaration or calling of a function which has two or more parameters, the use of comma is needed to separate the different parameters.  Particularly in Matlab (or in any other of its similar open source counterparts, such as Scilab, GNU-Octave, FreeMat... etc.) one function declaration could resemble this:

function [cm, kg] = anglo2metric(in, lb)
cm = in * 2.54;
kg = lb * 0.45359237;

 

You send parameters ‘in’ (for inches) and ‘lb’ (for pounds) to function ‘anglo2metric’. This function processes those numbers and return two values, in ‘cm’ (for centimeters) and ‘kg’ (for kilograms). You can choose the names of the input parameters and of the output values. 

A coded function is used to return some value or information.  Functions (made by you or built-in) do calculations, sure, but they’re also useful to indicate some errors that they encounter, let’s say for debugging purposes. 

On this page we’re not talking about any specific programming language. Functions do exist in every computer language. 

An example of script for PHP is the following: 

<?php
function add_numbers($var1 = 0, $var2 = 0, $var3 = 0)
{
  $var4 = $var1 + $var2 + $var3;
  return $var4;
} 

$sum = add_numbers(3,4,6)
echo “The result of 3 + 4 + 6 is {$sum}
?>

After execution, you get: 

The result of 3 + 4 + 6 is 13


Note that sometimes you need special characters (such as ‘{’ and ‘}’) to define your start/end of youre function, or the reserved words ‘return’ or ‘endfunction’ to end that portion of code. 

These sentences may look like a bunch of strange letters and numbers but these symbols actually account to make a certain task easier.  And that, for the moment, is the most important thing.

 From 'Programming Functions ' to home
 From 'Programming Functions' to 'Basics of Programming'
 
Top

Books on Programming

Books on algorithms




footer for programming functions page