English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I wrote a sub- in a particular cell, I display value of some calculations. It means there is an output.

But, the definition of sub says there is no output. Then, how does it differ from function?

2007-10-31 15:00:47 · 4 answers · asked by Kanchan G 2 in Computers & Internet Programming & Design

4 answers

Hi,
A sub(routine) can NOT return a value. But a function can return a value. Suppose "a_sub" is a subroutine and "a_fun" is a function. u want to call them then a subroutine is called only by writing it name
i.e a_sub (no parenthasis, only name)
and a function is called as:
b=a_fun() (with parenthasis)
as it always returns a value so its value must be saved in some variable.
These are examples when they do not carry any parameters.

Now see the example with parameters: say x and y are parameters, then

call a subroutine--> a_sub x,y (no parenthasis)
call a function--> b=a_fun(x,y) (with parenthasis)

However u can take output from a subroutine in a different way, Suppose u want to add x and y and it result in z variable. ur sub will be like this

sub add(x as integer,y as integer,z as integer)
z=x+y
end sub

Now call this subroutine
add x, y, z
when it will return result will be saved in z variable, so it has not returned anything.

Now see the function

Function add(x as integer,y as integer) as integer
add=x+y
end function

Now call this function
b=add( x, y)
it has returned total of x and y to b

2007-10-31 18:26:53 · answer #1 · answered by iqbal 4 · 0 0

Wrote a sub in a cell? Sub or formula?
Can you give us the detail syntax?

Different Sub(routine) and Function:
1. Sub will not return values
Function will return values

2. Sub can't called from cells
Functions can called from cells

2007-10-31 20:19:50 · answer #2 · answered by vijay 2 · 0 0

A Sub can pass back any number of parameter values or perform an operation using input parameters but no, only a Function returns a variable based on the input parameters but its input parameters are RO.

2007-10-31 15:04:46 · answer #3 · answered by Anonymous · 0 0

the reason why they likely had that changed into because it changed into likely used interior a With...end With actuality. the following is an person-friendly party of two code examples that would want to do an analogous aspect: Set rRange = Sheets("Sheet2").determination("A1") Then utilising the With...end With actuality, the following is doing an analogous aspect: With Sheets("Sheet2") Set rRange = .determination("A1") end With What it does is make something that begins out with a era act like that's extra onto the code contained in the With actuality. So in my party it merely grants on .determination("A1") to the end of Sheets("Sheet2"). it would want to seem as if more advantageous paintings and typing. And that's once you've merely one line of code it is functional to set. yet once you've dissimilar lines of code it would want to ward off a ton of time. i pick to apply With...end With lots at the same time as i'm writing a large macro for a mode from a module because you would possibly want to specify in each and every line the fashion's call or at the same time as i favor to specify values for most certain cells on a particular worksheet. the following is slightly of code the position I used it to take an action in accordance to a mix container determination: With frmPackagingCalc.cboCleaningProcedure If .ListIndex = 0 Then CleaningProc = a million ElseIf .ListIndex = a million Then CleaningProc = "X" ElseIf .ListIndex = 2 Then CleaningProc = "Z" ElseIf .ListIndex = 3 Then CleaningProc = Chr(216) end If end With for each line the position you spot the .ListIndex if I hadn't have used the With...end With i'd have had to form or a minimum of reproduction and paste: frmPackagingCalc.cboCleaningProcedure...... Haha...the line is so long that Yahoo solutions gained't even enable it is placed on one line. yet another aspect high quality about that's that it keeps the line of codes shorter and less complicated. and so as that they mak your macro run slightly faster. each and every "." that the computing gadget encounters on your code it has to make a calculation. I eliminated about 3 to 4 classes by technique of creating use of the With...end with actuality. additionally they make the code a lot less complicated to study. i understand in my code that all and multiple of those record indexes search for suggestion from the mixture container cboCleaningProcedure. If I had executed it any opposite direction, you would truly replace the call for one in all them to something else and by no skill understand, "Wait, it is pertaining to a very diverse mix container!"

2016-10-23 04:53:35 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers