Ask Your Question

Basic: what is returned in a function?

asked 2018-12-01 03:20:03 +0200

Todd2 gravatar image

updated 2018-12-01 16:08:44 +0200

erAck gravatar image

Hi All,

I am confused at to how functions work. I am reading: and con not figure out what is returned from the function.

I am comming from Perl 6. What is returned is

return value

But I can't tell what is returned. For instance:

Public Function FILEPATH() As String
   ' Returns file path excluding trailing separator.
   Dim u As String
   Dim i As Long
   u = ConvertFromURL( ThisComponent.Url )
   i = InStrRev( u, GetPathSeparator() )
   FILEPATH = Left( u, i - 1 )
End Function

If I was to take a wild guess, the return value is also the same as the function name (FILEPATH). What am I missing?

Many thanks, -T

Edit: can I return two values? If so, what is the syntax?

edit retag flag offensive close merge delete


Please tag your questions properly, which helps others to find related questions. I just added the basic tag to all your recent questions. Thanks.

erAck gravatar imageerAck ( 2018-12-01 16:11:33 +0200 )edit

I get the finger shaken at me when I try to use "basic". Maybe I mispelled it a lot

Todd2 gravatar imageTodd2 ( 2018-12-02 20:24:29 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2018-12-01 07:47:25 +0200

JohnSUN gravatar image

updated 2018-12-01 07:49:31 +0200

It is not difficult.

In fact, the operator "return value" does the same thing — it assigns a "value" to the "function name". This is just a different syntax, the meaning of the last line in the function is the same.

Let's look at a function that returns two values.

Function FilePathAndName() As Variant
' Returns file path excluding trailing separator and file name
' The result is an array of two elements.
Dim sTemp As String  ' temporary string variable - the URL of the current document
Dim aTemp As Variant ' temporary variable of the variant - later it will be an array
Dim sFileName As String, sFilePath As String ' the purpose of these variables is clear from their names
  sTemp = ConvertFromURL( ThisComponent.getUrl() )  ' get URL of current document
  If sTemp = "" Then ' if document is new (not saved) then URL is empty string
    sFileName = "file name undefined"
    sFilePath = "file path undefined"
    aTemp = Split( sTemp, GetPathSeparator() )
    sFileName = aTemp( Ubound(aTemp) ) ' last element of array is a part of string after last PathSeparator
    sFilePath = Left( sTemp, Len( sTemp ) - Len( sFileName ) - 1 ) ' rest of URL-string is path
  FilePathAndName = Array( sFilePath, sFileName ) ' pack both value to array and return it as function name
End Function

You can enter in cell {=FILEPATHANDNAME()} as an array formula and get in two adjacent cells two values ​​returned by the function.

Array function.png

You can use this function in your other functions and procedures. For example,

Function FilePath() As String
' Returns file path excluding trailing separator 
Dim aTemp As Variant
  aTemp = FilePathAndName()
  FilePath = aTemp(0) ' First element of array
End Function

Function FileName() As String
' Returns file name
Dim aTemp As Variant
  aTemp = FilePathAndName()
  FileName = aTemp(1) ' Second element of array
End Function

You can write each of these functions in a formula in a cell and get the desired result.

There is another way to return multiple values ​​from a single function — you need to list all returned values ​​as parameters by reference. In this case the function (usually, but not necessarily!) returns a boolean value - whether the execution was successful.

Function isFileNameGood(Optional ByRef sFileName As String, Optional ByRef sFilePath As String) As Boolean 
  sFileName = FileName()
  sFilePath = FilePath()
  isFileNameGood = (sFileName <> "file name undefined")
End Function

Of course, calling such a function from a table cell will not bring much benefit - you just get True if the current document is saved or False otherwise. You can use the calculated values ​​of the path and name of file only by calling such a function inside your code.

Good luck!

edit flag offensive delete link more


Thank you!

Todd2 gravatar imageTodd2 ( 2018-12-02 20:24:40 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-12-01 03:20:03 +0200

Seen: 2,220 times

Last updated: Dec 01 '18