Introduction to Functions

Functions

A function is a group of statements that exist within a program for the purpose of performing a specific task.

• A function is a section of reusable code that performs an action.
• A function has a name and is called, or executed, by that name.
• Optionally, functions can accept arguments and return data.
Function naming rules:
• Cannot use key words as a function name
• Cannot contain spaces
• Methods are fuctions run against an object.
• First character must be a letter or underscore
• All other characters must be a letter, number or underscore
• Uppercase and lowercase characters are distinct

BENEFITS OF MODULARISING A PROGRAM WITH FUNCTIONS

• Simpler Code: easier to understand when it is broken down into functions.
• Code Reuse: written once to perform that operation and then executed any time it is needed.
• Better Testing: can test each function in a program individually, easier to fix errors.
• Faster Development: library of functions can be incorporated into each program that needs them.
• Easier Facilitation of Teamwork : different programmers can be assigned the job of writing different functions.