Showing posts with label Procedure. Show all posts
Showing posts with label Procedure. Show all posts

Tuesday, March 27, 2012

Methods: Procedures & Functions

DOWNLOAD THE CODE: MethodsPkg

There are two types of methods:
  1. Procedure
  2. Function
All methods begin with a method signature which opens into a code block.

A method signature is a contract which specifies:

  • visibility (can it be seen externally?)
  • what type of data it will return
  • what it will do (the name)
  • which data, if any, it requires to perform it's task

visibility | return data type | method name | (parameters if any) {

}



Procedure
What it does: executes processing statements.

Example:



The method signature for a procedure always contains the keyword void (meaning nothing/empty). This is as opposed to a function, which always returns a value. Read on...


Function
What it does: executes processing statements and returns a value to the calling procedure.

Example:



The method signature for a function always contains a datatype to indicate the type of data that will be returned from the function.

The final statement in a function begins with the return keyword, followed by the data that will be returned. In this case, a String.

Methods may also require incoming values called parameters (sometimes called arguments) which are placed within the parenthesis following the method name.