Difference makes the DIFFERENCE
Categorised Menu List
What they are and How do they vary from the others
Let us first consider the dictionary meaning of Procedure and Function
- is pronounced as pro-cee-jar:
Generic meaning: a series of actions that are done in a certain way or order: an established or accepted way of doing something
Computer parellance: a set of instructions for a computer that has a name by which it can be called into action
The only difference between a PROCEDURE and a FUNCTION is that,
in some languages, a procedure may return more than one value.
- is pronounced as func-tion
Generic meaning: the special purpose or activity for which a thig exists or is used job or duty of a person a large ceremony or social event
Computer parellance: a computer subroutine: specifically: the one that performs a calculation with variables provided by a program and supplies the program with a single result.
Now let us consider the five basic questions What, Why, When, How
What - A Procedure is a part of a programme, that does a specified activity
Why - Procedure is used to simplify a program, inturn, reducing number of lines
When - the same activity is performed more than once, we use a procedure
How - by seperating those lines of code and giving them a seperate name.
Henceforth, we can consider Procedure and Function to be in alternative usage, when ever, you come across procedure, function is also implied, with exception to the know difference of a function returning a value.
Every Programme starts with a PROCEDURE and many times ends with the SAME PROCEDURE. For that matter, every program is a procedure or collection of procedures.
In VBA, Procedure looks like the following examples:
Keywords PUBLIC, PRIVATE will be discussed in 'Scope declaration context'
Words in square braces are optional, words in angular braces are compulsory.
Every procedure starts with 'Sub' or 'Private Sub' and ends with 'End Sub'.
Visual Basic Editor places End Sub automatically.
When Sub is changed to Function, editor automatically changes End Sub to End Function.
It is customary but not mandatory to include 'prc' as a prefix for any procedure and 'fnc' as a prefix to any Function, this helps in distinguishing procedures and functions and also between system functions or procedures.
It is customary to name procedures with meaningful names
arguments list should be seperated by commas
This example, does not take any input nor return any value. It just does the given task (which is included between 'Private Sub' and 'End Sub'
This example, does takes two input variables but does not return any value. It just does the given task (which is included between 'Private Sub' and 'End Sub'
This example, does not contain key word 'Private'. It takes one input variable but does not return any value. It just does the given task between 'Sub' and 'End Sub'
In this example - i, j and k are called Global Variables. Initially, they are stored with 'zero' value. Procedure prcMain initialises i with 5, j with 15 and calls prcAddNumber procedure. This second procedure adds given input and stores the value in variable k. Procedure 'prcAddNumber' starts with 'prcMain' and ends with 'prcMain'
Please note: Function has a return type in declaration
Structure: Private Function fncCalDifference(a as integer, b as integer) as integer
Example 5: Functions and Procedures can be used together in the same program. ie., Function can be used in another Function or a Procedure can be used in a Functon or a Procedure can be used in a Procedure or a Function can be used in another Procedure
All the content that you learn here will not be of any use, until you practice, so I urge you to practice. As and when you practice, you get more doubts as to how to go ahead, these doubts strengthen your knowledge and lead you to experience. These will be helpful in day-to-day activities.
Application.StatusBar = "Formatting current sheet to Standard Formatting..........."
.Name = "Calibri"
.Size = 10
Selection.RowHeight = 20.25
.VerticalAlignment = xlCenter
.ColumnWidth = 8.57
Rows("1:1").RowHeight = 40.5
.VerticalAlignment = xlCenter
.WrapText = True