Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Functions 101  The basics

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
wbntravis - 15 Feb 2007 08:10 GMT
I am new to VB programming  I am used to writing functions in other languages
like Perl...   ususally the last line or a return $A statement defines what is
returned by the function.  The few vba functions I have seen confuse me as I
have no indicator of what the function will return.

On this function.
JE McGimpsey - 15 Feb 2007 08:35 GMT
From XL/VBA Help ("Writing a Function Procedure"):

> A function returns a value by assigning a value to its name in one or
> more statements of the procedure.

So

   Public Function foo(byVal bar As Double) As Double
       foo = bar + 1
   End Function

> I am new to VB programming  I am used to writing functions in other languages
> like Perl...   ususally the last line or a return $A statement defines what is
> returned by the function.  The few vba functions I have seen confuse me as I
> have no indicator of what the function will return.
>
> On this function.
Martin Fishlock - 15 Feb 2007 08:45 GMT
Hi Travis:

There  are two types of procedure in VBA, a function which returns a value
and a subroutine which cannot return as value.

Although the subroutine can modify variables by using the 'byref' (default )
variable.

A subrountine is used to do a task with no return like format a report or
some other task where no confirmation of success is required where as a
function is a process that returns a value like len(a)

A function proper in VBA is specified as :

FUNCTION functionname(
[byval  | byval] arg1 as type1 [,  [byrefsubroutine | byval] arg2 as
type2.....]* ]
) as typen

' code in here
functionname = somevalue
end function

the
functionname = somevalue can be anywhere in the function and you can exit
the function in the middle of the code using the exit function.

In certain cicrmustances more than one variable is returned and then you
need to use globals or byref (like pointers in c(++)) so I could write a
function

function splitinhalf(byval s as string, byref s1 as string, byref s2 as
string) as boolean

and this function whould split the string s in half and put the answers in
s1 and s2 and return true on success and false otherwise.

Here is an example :

function whatnumberisit(byval n as long) as string
if n=1 then
whacircumstancestnumberisit="onwoulde"
exit function ' this one exits here
endif
if n=2 then
whatnumberisit="two"
' this one drops to the bottom
elseif  n=3 them
 whatnumberisit="three"
exit function ' this one exits here
else
 whatnumberisit="unknown"
' this one drops to the bottom
endif
' here we exit if no exit before
endif

so you see that there a different ways of exiting the function.

I would recommend getting a book out the library or buying a book and
working through it it will help a lot more than patching Internet help
together.

Signature

Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.

> I am new to VB programming  I am used to writing functions in other languages
> like Perl...   usually the last line or a return $A statement defines what is
> returned by the function.  The few vba functions I have seen confuse me as I
> have no indicator of what the function will return.
>
> On this function.
Bob Phillips - 15 Feb 2007 09:36 GMT
certain cicrmustances more than one variable is returned and then you
> need to use globals or byref (like pointers in c(++)) so I could write a
> function
[quoted text clipped - 4 lines]
> and this function whould split the string s in half and put the answers in
> s1 and s2 and return true on success and false otherwise.

or the function could return an array
Martin Fishlock - 15 Feb 2007 10:40 GMT
Well I would actually prefer to return a user defined type (UDT) but I didn't
really want to bring in UDT for a discussion on functions. It is a little
safer than arrays.

Arrays are fine but you then have to process them and deal with limits etc.
But in some situations an array would be useful like split a line of text
into words.
Signature

Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.

> certain cicrmustances more than one variable is returned and then you
> > need to use globals or byref (like pointers in c(++)) so I could write a
[quoted text clipped - 7 lines]
>
> or the function could return an array
Bob Phillips - 15 Feb 2007 11:11 GMT
IMO UDTs are a good idea, but badly implemented in VB. If you are a serious
VB programmer, arrays are one of your basic tools, so handling them should
be meat and drink.

Signature

---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

> Well I would actually prefer to return a user defined type (UDT) but I
> didn't
[quoted text clipped - 19 lines]
>>
>> or the function could return an array
Chip Pearson - 15 Feb 2007 14:07 GMT
> IMO UDTs are a good idea, but badly implemented in VB.

Very well put. I never use UDTs except when required by an API call.  The
limitations on use UDTs with Collections and For Each loops make the barely
useful. I always use classes instead.

A Collection or Array of Class instances is much more manageable and
flexible than UDTs.

Signature

Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

> IMO UDTs are a good idea, but badly implemented in VB. If you are a
> serious VB programmer, arrays are one of your basic tools, so handling
[quoted text clipped - 24 lines]
>>>
>>> or the function could return an array
wbntravis - 15 Feb 2007 09:41 GMT
Thanks Martin & JE
You both approached it differently so I caught the idea.
I agree on the book Martin.  For now I have been using global
variables for the return but that did not seem the real approach I should be
taking.  It was more of a quick fix.
I also found this page helpful
 

> Hi Travis:
>
[quoted text clipped - 65 lines]
> >
> > On this function.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.