Function vs. Procedure

dave8

Active Member
Joined
Jul 8, 2007
Messages
275
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?
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,021
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top