Functions v Procedures

confusion123

Active Member
Joined
Jul 27, 2014
Messages
400
The main (possibly only) difference between a function and a procedure is a function can return values whereas procedures can't.

Therefore why use procedures at all? Why not just write functions and return values if you want, don't if you don't need to?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Why write a function that doesn't return a value? Why not distinguish between routines that do/don't return values? In what way would it be better not to?
 
Upvote 0
Just thought of simplifying matters, ie if functions do everything procs do and more, you only need to learn 1 thing instead of 2.
 
Upvote 0
Well, it's your code, so you can write it however you like.
 
Upvote 0
The main (possibly only) difference between a function and a procedure is a function can return values whereas procedures can't.
Where did that 'fact' come from?
Could you write a function to ..
- add a worksheet to a workbook?
- delete column J?
- put borders around each cell in the range B5:D8?
- open all the excel files in a folder and put a 1 in cell A1 of the first worksheet?
- etc
 
Last edited:
Upvote 0
Short answer, it's language design.

An excel sub can be executed directly, a function can only be called by another procedure or as a User Defined Function. Try writing a function to delete rows that you can call with a keyboard shortcut.

Some scripting languages only have functions: everything returns a value.
 
Upvote 0
In fact, you've given your own answer. You can relatively consider functions like custom "Method"s.
Think you need a custom check like "if a boolean array contains a true value". There is no such standard method to check this as far as I know.

So there are two ways to do it:
Procedural Way
Code:
Sub Procedure (
Boolean check = False
For Each i as Boolean In B[]
If B(i) is True Then
check = True
End If
Next
If check is True Then 
'Do something
End If
)
With Function
Code:
Function checkArrayContainsTrue(ByRef array[]) As Boolean (
For Each i as Boolean In B[]
If B(i) is True Then
Return True
End If
Next
)

Sub Procedure (
If checkArrayContainsTrue(B[]) Then 
'Do something
End If
)
.
Sorry for my syntax. I have been dealing with C++ for a long time and forgot some of the correct VB notation but i guess, you've got the idea.
If you use only procedural statements you have to write this kind of a check in every sub procedure. But if it is defined once in a function, then, you can call it anywhere, anytime as you like. A more celan way let's say. Also in a long term it may lead you to learn Object Oriented Programming.
 
Upvote 0
Where did that 'fact' come from?
Could you write a function to ..
- add a worksheet to a workbook?
- delete column J?
- put borders around each cell in the range B5:D8?
- open all the excel files in a folder and put a 1 in cell A1 of the first worksheet?
- etc

Yes, very easily! Write a subroutine to do it then change the word Sub to Function. :)
 
Upvote 0
a function can only be called by another procedure or as a User Defined Function

That's not entirely true. Functions can be run from the macros dialog (you have to type the name in yourself) and assigned to shapes.
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,382
Members
448,889
Latest member
TS_711

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