Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Functions 101 The basics

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.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Functions 101 The basics

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


In article ,
wbntravis wrote:

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.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Functions 101 The basics

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.



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


"wbntravis" wrote:

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.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,726
Default Functions 101 The basics

"Martin Fishlock" wrote in message
...

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.



or the function could return an array


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default Functions 101 The basics

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


"Martin Fishlock" wrote:

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.



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


"wbntravis" wrote:

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.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 694
Default Functions 101 The basics

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.
--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


"Bob Phillips" wrote:

"Martin Fishlock" wrote in message
...

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.



or the function could return an array



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,726
Default Functions 101 The basics

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.

--
---
HTH

Bob

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



"Martin Fishlock" wrote in message
...
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.
--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


"Bob Phillips" wrote:

"Martin Fishlock" wrote in message
...

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.



or the function could return an array





  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Functions 101 The basics

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.

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

"Bob Phillips" wrote in message
...
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.

--
---
HTH

Bob

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



"Martin Fishlock" wrote in message
...
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.
--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


"Bob Phillips" wrote:

"Martin Fishlock" wrote in message
...

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.


or the function could return an array







Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
basics? n21 New Users to Excel 7 September 21st 06 12:37 PM
QueryTable Basics.. thiaga Excel Programming 0 March 9th 06 02:42 PM
Hi I'm New and need help with Excel Basics abi Excel Discussion (Misc queries) 10 November 23rd 05 07:25 PM
VBA form basics Colin Foster[_4_] Excel Programming 0 August 4th 05 12:29 PM
Free books on Charting, Basics, Data Management, Functions; Free Add-In "Excel Usability Enhancer" VJ[_3_] Excel Programming 0 December 11th 03 09:01 AM


All times are GMT +1. The time now is 02:51 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"