Function vs. Procedure

dave8

Active Member
Joined
Jul 8, 2007
Messages
264
Can someone tell me the difference between a Function and Procedure? From what I understand is that a Function returns a value. Can a Procedure return a value? How?
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

SidBord

Active Member
Joined
Aug 23, 2004
Messages
346
The primary effect of a function is to behave like a variable, except the function must do some processing whereas a variable can’t do any processing. One other MAJOR difference is that a macro function MUST NOT do anything that changes anything on a worksheet!! A function can otherwise do just about any kind of processing that a procedure can. It MUST however execute a statement that returns a value. For example, if you write a function named “GetThisSheetName”, somewhere in the code you MUST have a statement something like this:

GetThisSheetName = “Name of Sheet”
and that statement must be executed before exiting the function. Then, if used in a worksheet formula, you might write something like:
= Call GetThisSheetName()
Note that you MUST use parentheses following a function name that’s referenced in a formula. When used in a macro, the function name does not require the parens and you don’t need the “Call”. I suggest you look at help for the rules about arguments.

A procedure can return one or more values by using arguments. Normally arguments are used to pass information “to” a macro procedure, but you can use an argument to return a value ”from” the procedure to the caller if convenient.

One other thing to consider is results of an error that occurs in a function. Since a function MUST always return a value, you should provide a returned value that tells the calling routine a disaster has occurred. If your function normally returns a positive integer as a value, then you could return -1 if an error occurs.
HTH, Sid
 

Forum statistics

Threads
1,089,197
Messages
5,406,761
Members
403,105
Latest member
gituncar

This Week's Hot Topics

Top