Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returning vars

Status
Not open for further replies.

drewdaman

Programmer
Aug 5, 2003
302
CA
so, my next question is a very fundamental question, and when i google it, i get weird results making me think that it might not be possible.. but that also seems strange!

how can you return a value from a function? so far, since i couldn't find the syntax to do it, i have been passing variables by reference and modifying them. can someone please show me the syntax to return a variable?

c++:
rettype funcname (params){
rettype a;
//operations;
return a;
}

i'm not sure how to state teh function should return this type of value, and what the syntax for returning is... when i google, i just keep getting a whole bunch of stuff on return on investment.. and nothing on returning variables.
 
Function myFunction(...) As SomeType
....
myFunction = someValue
End Function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
oh jeez... i'm such a newbie!

it seems that a function can return a value and a sub cannot. is that correct? are there any other differences between subs and functions that i should keep in mind while choosing to use one or the other?

thanks!
 
Technically, a Sub does not return a value. However, as you have already discovered, you can have a Sub modify a parameter passed ByRef and your calling procedure will "see" this value.


Regards,
Mike
 
thanks!

i just read soemwhere that there is one thing that functions can't do and that subs can do.. i didn't quite understand that and i was hoping you could shed some light on this:

That’s it, [functions] just return a value. That means that they can’t modify the user interface like a Sub can. They can’t, for instance, write a value to a cell EXCEPT for the cell in which they’re entered.
 
In other words, a VBA function used on a worksheet cannot make any changes to other user interface elements (cells, worksheets, etc.). All it is allowed to do is return a value to the cell that references it.


Mike
 
This limitation is only for functions called in a cell formula.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Functions in a module that are called from code can change anything they want - but they don't show up in the Macros dialog box

Rgds, Geoff

Three things are certain. Death, taxes and lost data. DPlank is to blame

Please read FAQ222-2244 before you ask a question
 
thanks! that clears things up! will get back with more questions when i have them! thanks everyone for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top