UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> (Beginer Question #3) Passing a value to a function    
 
   
AntonioPS
post Oct 23 2007, 11:38 AM
Post #1

UtterAccess Addict
Posts: 241
From: DE and PA



I like to try and call functions where i can for individual processes. OOP?

How can i pass a value from where the function is being called to the actual function

ie

Sub
call fn
myvalue= .value
end sub

function
use.value in code
Go to the top of the page
 
+
MattJ
post Oct 23 2007, 11:43 AM
Post #2

UtterAccess VIP
Posts: 4,621
From: North Carolina, USA



An example function to add two numbers together;

Function AddNums(intNum1 as Integer, intNum2 as Integer)
AddNums = intNum1 + intNum2)
End Function

Notice the arguments for the function in the first line. Then to call this function, it would be

AddNums(1,2)

The result would be 3. You wouldn't have to directly enter numbers, it also could be a variable or a form reference.
Go to the top of the page
 
+
Larry Larsen
post Oct 23 2007, 11:49 AM
Post #3

UA Editor + Utterly Certified
Posts: 22,722
From: Melton Mowbray,Leicestershire (U.K)



Hi
Matt missed of a "(" from the function..
CODE
Function AddNums(intNum1 as Integer, intNum2 as Integer)
AddNums = [color="red"]([/color]intNum1 + intNum2)
End Function

HTH's
(IMG:http://www.utteraccess.com/forum/style_emoticons/default/thumbup.gif)
Go to the top of the page
 
+
AntonioPS
post Oct 23 2007, 11:50 AM
Post #4

UtterAccess Addict
Posts: 241
From: DE and PA



hmm maybe i am using the wrong terminology.

so there are public and private subs as well as public and private functions. or at least that is what my book tells me.

i want to pass a variable from a sub to a function?

or do i have to declare the variable again in the function and look up the value i want again from a form?
Go to the top of the page
 
+
MattJ
post Oct 23 2007, 11:55 AM
Post #5

UtterAccess VIP
Posts: 4,621
From: North Carolina, USA



You could declare a global variable - then you could set it and retrieve the value from anywhere. Usually in functions, the variables that are passed are arguments to the function (as shown above).
Go to the top of the page
 
+
AntonioPS
post Oct 23 2007, 12:03 PM
Post #6

UtterAccess Addict
Posts: 241
From: DE and PA



ahh i see. sorry had to look up global variable.

I would place this at the top

Option Compare Database
Public myvariable As String
Option Explicit

In a sub i would first set "myvalue" = nothing then give it a value and call the function

Then in the Function i would use it by simply using myvalue in my code

is that correct?
Go to the top of the page
 
+
MattJ
post Oct 23 2007, 12:05 PM
Post #7

UtterAccess VIP
Posts: 4,621
From: North Carolina, USA



It depends on the function, but yes...
Go to the top of the page
 
+
AntonioPS
post Oct 23 2007, 12:08 PM
Post #8

UtterAccess Addict
Posts: 241
From: DE and PA



am i missing something? when should i be using a sub versus a function?
I am reading your response as if i am just not getting it.
Go to the top of the page
 
+
jasonlewis
post Oct 23 2007, 12:42 PM
Post #9

UtterAccess Addict
Posts: 201



Hi Antonio,

1) The general difference between a procedure (Sub) and a function (Function) is that a function is conventionally supposed to return a value, where a procedure cannot return a value.

2) As to passing and updating variables from within a function or procedure, it depends on how you pass them. By default, variables passed as arguments to functions can be modified (e.g. they are passed by reference).

CODE
  
[color="green"]' A Procedure To Increment A Value Passed As An Argument[/color]
Public Sub MyIncrementProc(Value As Integer)
  Value = Value + 1
End Sub

[color="green"]' A Function To Increment A Value Passed As An Argument[/color]
Public Function MyIncrementFunc(Value As Integer) As Boolean

  On Error Goto Err_MyIncrementFunc
  Value = Value + 1
  MyIncrementFunc = True

Exit_MyIncrementFunc:
  Exit Function
Err_MyIncrementFunc:
  MsgBox "MyIncrmementFunc ERROR" & Err.Description
  Resume Exit_MyIncrementFunc
End Function


[color="green"]' A Function To Increment A Value Passed As An Argument (By Value)[/color]
Public Function MyOtherIncrementFunc(ByVal Value As Integer) As Integer

  On Error Goto Err_MyOtherIncrementFunc
  MyOtherIncrementFunc = Value + 1
  Value = Value + 100 '<--this won't do anything to callers variable Value because it is passed in ByVal not ByRef (default)


Exit_MyOtherIncrementFunc:
  Exit Function
Err_MyOtherIncrementFunc:
  MsgBox "MyOtherIncrmementFunc ERROR" & Err.Description
  Resume Exit_MyIncrementFunc
End Function


Public Sub Test()
  Dim iValue As Integer
  Dim iTemp As Integer
  iValue = 1

  Debug.Print "iValue=[" & iValue & "]"
  Call MyIncrementProc(iValue)
  Debug.Print "iValue (after increment procedure)=[" & iValue & "]"

  If MyIncrementFunc(iValue) Then
    Debug.Print "iValue (after increment function)=[" & iValue & "]"
  End If

  iTemp = MyOtherIncrementFunc(iValue)
  Debug.Print "iValue (after other increment function)=[" & iValue & "]"
  Debug.Print "iTemp (after other increment function)=[" & iTemp & "]"
  
  
[/color]

  
End Sub


Hope this is useful,

Jason
Go to the top of the page
 
+
MattJ
post Oct 23 2007, 12:43 PM
Post #10

UtterAccess VIP
Posts: 4,621
From: North Carolina, USA



The general difference between sub and functions is that functions produce a value. Subs merely process lines of code.
Go to the top of the page
 
+
AntonioPS
post Oct 23 2007, 01:03 PM
Post #11

UtterAccess Addict
Posts: 241
From: DE and PA



Oh wow okay I really get it now.

I should probably rename my functions as subs then because they do not produce a value.

Thank you Matt and Jason!

Jason- after your procedure name you fill things in the ( )

what is that called? the are in between the brackets

Public Function MyOtherIncrementFunc(ByVal Value As Integer) As Integer

i want to look it up. up until now i havent used it myself
Go to the top of the page
 
+
jasonlewis
post Oct 23 2007, 01:14 PM
Post #12

UtterAccess Addict
Posts: 201



Hi Antonio,

The items within the parenthesis of a Function or Procedure are called arguments (sometimes they are also called parameters).

In the Office Online help you can search on 'About procedures' and find the reference on syntax.

Hope this helps,

Jason
Go to the top of the page
 
+

Thank you for your support! Reply to this topicStart new topic

Jump To Forum:
 



RSS Go to Top  ·  Lo-Fi Version Time is now: 19th May 2013 - 12:14 AM