How can we return values from userdefined function? anybody provide code with small example its great for me.

Showing Answers 1 - 7 of 7 Answers

seleena

  • May 15th, 2006
 

The syntax of a Function:

[Public [Default] | Private] Function name [(arglist)]   [statements]   [name = expression]   [Exit Function]    [statements]   [name = expression]End Function 

Arguments

Public
Indicates that the Function procedure is accessible to all other procedures in all scripts.
Default
Used only with the Public keyword in a Class block to indicate that the Function procedure is the default method for the class. An error occurs if more than one Default procedure is specified in a class.
Private
Indicates that the Function procedure is accessible only to other procedures in the script where it is declared or if the function is a member of a class, and that the Function procedure is accessible only to other procedures in that class.
name
Name of the Function; follows standard variable naming conventions.
arglist
List of variables representing arguments that are passed to the Function procedure when it is called. Commas separate multiple variables.
statements
Any group of statements to be executed within the body of the Function procedure.
expression
Return value of the Function.

Note: So, to return a value, assign the return value to the function name.

For Eg:

Function BinarySearch(. . .)
      . . .
      ' Value not found. Return a value of False.
      If lower > upper Then
            BinarySearch = False   
            Exit Function
      End If
      . . .
End Function

  Was this answer useful?  Yes

akothuru

  • Jun 6th, 2008
 

Function can return only one value
To return value, use the following syntax

E.g.
Function FunctionName()
     FunctionName= True
End Function


FunctionName = True 'Boolean value
or
FunctionName = "Abcd" 'String
or
FunctionName = var_i 'variable

If you want to return multiple values, you should use ByRef peremeters. By Default, the function parameter type is ByVal. The parameters declared as ByRef can only send back the values to script which are updated inside function 
a =4
b =5
msgbox "a: " & a 'diaplay 4
msgbox "b: " & b 'diaplay 5
Call abcd(a,b)
msgbox "a: " & a 'diaplay 5
msgbox "b: " & b 'diaplay 4
  
Function abcd(ByRef a, ByRef b)
   temp = a
   a = b
   b = temp
   abcd = True
End Function





  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions