HTML Blox

Providing the building blox of the web.

  • HTML Blox Home
  • Blog
    • Design
    • Flash
    • General
    • HTML
    • Javascript
    • PHP
    • Programming
    • Web Servers
  • Forums
  • Where to Start?
  • Lessons
    • CSS
    • Design
    • HTML
    • Javascript
    • Programming
  • References
    • HTML
  • Resources
  • About
  • Contact
  • Go Green!

Programming Lessons

  1. Lesson 1: The Basics
  2. Lesson 2: A Brief History of Computers
  3. Lesson 3: The Workings of Computers
  4. Lesson 4: A Brief History of Programming
  5. Lesson 5: High-Level vs. Low-Level
  6. Lesson 6: Compiled vs. Interpreted
  7. Lesson 7: Variables
  8. Lesson 8.1: Arithmetic and Operators - Decimal
  9. Lesson 8.2: Arithmetic and Operators - Integer and Boolean
  10. Lesson 9.1: Conditional Statements - The Basics
  11. Lesson 9.2: Conditional Statements - Branching Conditionals
  12. Lesson 10.1: Loops - The Basics
  13. Lesson 10.2: Types of Loop
  14. Lesson 11: Functions
  15. Lesson 12: Strings
  16. Lesson 13: Procedural vs. Object-Oriented

Programming Reference

  1. No reference yet.

Links

  • W3C HTML 4.01 Strict Compliant
  • W3C CSS 2.1 Compliant
  • Web Standards Group Member
  • Follow us on Twitter
  • Number Overflow - Free Advanced Scripts and Tutorials
  • Azure Ronin Web Design - Professional Web Design and Development
  • XML Sitemap
  • U Comment, I Follow

Feeds

  • RSS 2.0 Feed
  • RDF/RSS 1.0 Feed
  • RSS 0.9 Feed
  • Atom Feed

Lesson 11: Functions

del.icio.us Digg Blinklist reddit

In our last lesson we took a look at conditional statements. In this lesson we’ll take a look at functions, which will let us develop better, larger, and more flexible code.

What are Functions?

Functions are essentially a programming structure that takes in input and returns output. Functions are also called procedures and subroutine, all of which refer to essentially the same thing.

You’ve likely seen functions before, in a mathematical context, in school. I think I remember seeing them as early as third grade. Have you ever seen an illustration like this:

Function

Function

where you are give some values pairs such as x = 5, y = 7, x = 7, y = 9, x = 9, y = 11 and you are told to figure out what the function is (in this case, it’s be y = x + 2). If you have, that’s a function, in a form often given to elementary students.

Later on, probably in high school algebra classes, you usually see something such as f(x) = 5x + 1, where you send in an x and get an answer. So, f(5) = 5(5) + 1 = 26.

Functions in programming are very similar. The only real difference is that you can send in zero or more arguments (depending on your function). These values are called “parameters”. Also, a function doesn’t necessarily have to give a value back (some languages call these procedures or subroutine, to differentiate between functions which return values).

Anatomy of a Function

The exact anatomy of a function differs from language to language, but generally they have several specific. Here is an example function:

Function Integer sum(Integer a, Integer b)
    Return a + b
End Function

Here is an example call to our function:

Integer total = sum(5, 6)
Print total

Total would print “11″.

Name

In every language I know of, a function must have a name. This is the name used to call the function. In our math example, f(x) = 5x + 1, f would be the name of the function. In our example function, sum would be the function name.

Parameters and Arguments

Functions contain parameters and arguments. In our f(x) = 5x + 1 example, x is the parameter. It is the reference to the value which you must supply to use the function. The argument is the actual value that is supplied when the function is used, so if we use f(5), 5 is our actual parameter.

Using our example function, a and b would be our parameters. The parameters must both be Integers, as specified right before the parameter names. In our example function call, 5 and 6 would be our arguments.

Parameters are sometimes called “Formal parameters”, with arguments being called “actual parameters”. These definitions are more from the mathematical version of functions.

Body

A function must have a body, which is the actual code that that is performed when the function is called. In our example, the body is just one line, which is “Return a + b”. The body has no limit on the number of lines it can contain in it’s body (it can even be zero, though the function would do nothing). However, a good rule is if you can’t see the entire function without scrolling your text editor, it’s too big and should probably be broken down into smaller functions.

Return Value

The return value isn’t a requirement (a function usually can return nothing). However, usually if it does return a value it must be of a certain type (know as the return type). In our example, the Integer between the words “Function” and “sum” specifies that we return an integer.

We use the word “Return” and then put the value it actual returns, which is “a + b”.

Using Functions

Now that we know the parts of the function, let’s talk about actually making use of them.

Generally, you want to break your code down into functions so you can reuse the code. Our sum function is a bit too simplistic to make legitimate use of, so let’s come up with something more practical. Let’s say that we want to write our own power function (so we can do stuff like 2 to the power of 3).

To do this, we’ll use a for loop to multiple our value multiple times (remember 2 to the 3rd is the same as 2 * 2 * 2). We’ll name the function “power” and we’ll call our parameters x and y.

Function Integer power(Integer x, Integer y)
    Integer returnValue = 1
    For Integer i = 0; i < y; i = i + 1
        returnValue = returnValue * x
    Return x
End Function

Now, whenever we want to get a power value, we just call our function, instead of having to create a whole for loop each time.

Print power(5,2)
Print power(2,3)
Print power(3,4)

These lines would print "25", "8", and "81" respectively.

We've just used Integers, but you can use any type of variables. Also, a function doesn't have to return a value. Let's say you are working on a video game and you want to create a function to increase your score by 100 each time it is called, and if we total 1000 points, we should get an extra life. This wouldn't require you send in any parameters, but it would still do something useful.

Differences between Functions, Subroutines, and Procedures

In most languages, there is no difference between functions, subroutines and procedures, they just lump them all in as "function". In other languages, such as Visual Basic, they make a differentiate: functions must return a value while subroutines can't.


In this lesson we took a look at functions; their anatomy and their basic usage. In our next lesson we'll take a look at some of the more useful string functions.

This entry was posted on Wednesday, November 11th, 2009 at 4:07 pm and is filed under Programming Lessons. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Please Login or Register or fill out the following to leave a reply:

If you haven't posted an approved comment before, your comment will not show up until approved.

  • HTML Blox Home
  • Blog
  • Design Blog
  • Flash Blog
  • General Blog
  • HTML Blog
  • Javascript Blog
  • PHP Blog
  • Programming Blog
  • Web Servers Blog
  • Forums
  • Where to Start?
  • Lessons
  • CSS Lessons
  • Design Lessons
  • HTML Lessons
  • Javascript Lessons
  • Programming Lessons
  • References
  • HTMLReferences
  • About
  • Resources
  • Contact
  • Go Green!
  • Sitemap

Copyright © 2008 HTML Blox

Unless otherwise noted, all images created and copyright HTML Blox.

Operated by Azure Ronin Web Design