Welcome to Dream.In.Code
Become a VB Expert!

Join 137,383 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,115 people online right now. Registration is fast and FREE... Join Now!




VB Functions

 
Reply to this topicStart new topic

VB Functions, Need help declaring function and passin parameters

dwd3773
8 Oct, 2008 - 05:18 PM
Post #1

D.I.C Head
**

Joined: 9 Feb, 2008
Posts: 103



Thanked: 1 times
My Contributions
I am working on an assignment for a class I'm taking. I need to create a sub procedure that will call a function. Set up parameters to pass data between the sub and function. Also, incorporate optional parameters into my procedure. I have some code written, but what I'm having trouble with is how to call the function from the sub procedure. This is what I have so far:

CODE

Option Compare Database

Sub functionTest()
    Dim strFirstName As String
    Dim strMiddleName As String
    Dim strLastName As String
    Dim strMessage As String
    
    strFirstName = "Derrick"
    strMiddleName = "W."
    strLastName = "Delancey"
    strMessage = "I hope I'm doing these correctly"
    
    testMessage(strFirstName, strMiddleName, strLastName, strMessage)
    
End Sub

Function testMessage(strFirstName, strMiddleName, strLastName, strMessage)

End Function


User is offlineProfile CardPM
+Quote Post

thava
RE: VB Functions
8 Oct, 2008 - 06:11 PM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Apr, 2007
Posts: 456



Thanked: 18 times
Dream Kudos: 50
My Contributions
in any language you must specified the data type of a variable change like this

Function testMessage(strFirstName as string, strMiddleNameas string, strLastName as string, strMessage as string)

End Function

for more information about functions follow this link

http://en.wikiversity.org/wiki/Functions_a...routines_in_VB6

User is offlineProfile CardPM
+Quote Post

akhileshbc
RE: VB Functions
11 Oct, 2008 - 03:46 AM
Post #3

D.I.C Head
Group Icon

Joined: 26 Sep, 2008
Posts: 177



Thanked: 3 times
Dream Kudos: 50
My Contributions
What thava had told you is right.
Also, use:

testMessage strFirstName, strMiddleName, strLastName, strMessage

If the function is not returning a value.
If the function returns smoething, then use the brackets.

For eg:

Function kk(abc as string) as string
kk=abc & " 12345"
end function

In the above function you need to use the brackets.
==

Function kk(abc as string)
msgbox abc & " 12345"
end function

In the above function, there is no need of brackets on calling the function ....

I hope you uderstand... smile.gif




User is offlineProfile CardPM
+Quote Post

jens
RE: VB Functions
11 Oct, 2008 - 11:32 AM
Post #4

D.I.C Head
Group Icon

Joined: 9 May, 2008
Posts: 119



Thanked: 4 times
Dream Kudos: 150
My Contributions
Use a function when you want to perform somthing and return a result.
Use a sub when you want to perform somthing but don't want to return a result.

Example, you want to change somthing in the forms apperace:

vb
private sub ChangeSomeStuff(colorParam as integer, heightParam as single)
'The sub is named ChangeSomeStuff, it takes two parameters - data sent to the sub.
'They are colorParam which must be a integer and heightParam that must be a single.
form1.bgcolor = colorParam
form1.heigth = heightParam
end sub


Now, if you want to return somthing, lets say you want to calculate the Kelvin temperature from Celsius:

vb
private function CtoK(cTemp as double) as single
' The above line says it's a function available in this form, it's name is CtoK,
' it takes one parameter - a double and it does return one single.
CtoK = cTemp + 273.15
end function

'Somewhere in your program...
kelvinTemperature = CtoK(celsiusTemperature)


This should show how to use functions and parameters.

/Jens
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 01:57AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month