Function procedure

On this one , I want to do 2 things
1) write a function procedure that receives the value stored in an integer variable named intNumber. The procedure, named DivideNumber, should divide the integer by 2, and then return the result (which may contain a decimal place)
2)write an appropriate statement to call the Dividenumber function created above.

Any help would be greatly appreciated... thx.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Here you have one solution:

Private Function DivideNumber(ByVal Number As Integer) As Double
Dim result As Double

result = (Number / 2)

Return result
End Function

to call the functio you must do the following:

Dim number As Double
Dim intNumber As Integer

intNumber = 2
number = DivideNumber(intNumber)

That's it.
0
Distinguished EngineerCommented:
This sounds like homework and experts answering those questions can get suspended..  you better give it a try at least, as it wont be too difficult for you.... have some problem ask us...
0
Author Commented:
its not homework.... these are just practice for us. What I am trying to get, is an example simple just like this, and then try to remember it and see how they do it.
In other words, its optional for me to practice... practice gets you better.
0
Commented:
FYI .....
Hi,

Here is where I found good examples for working in VB.Net

0
Commented:
in VB.NET this can be wriiten as:

Private Function DivideNumber(ByVal Number As Integer) As Double

return Number / 2

End Function

AW
0
Commented:
then to use this function:

Private Sub UseFunction()

Dim intTest as Integer = 100
Dim dblAnswer as Double = DivideNumber(intTest)

MessageBox.Show("DivideNumber(" & intTest & ") = " dblAnswer)

End Sub

Private Function DivideNumber(ByVal Number As Integer) As Double

return Number / 2

End Function
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
oops, minor type:

MessageBox.Show("DivideNumber(" & intTest & ") = " & dblAnswer)
0
Author Commented:
but, dont we have to add a button?
0
Middle School Assistant TeacherCommented:
Hi skathari,

You didn't ask for a GUI on top of the function.  You simply asked for a function to do X.  The button code is not part of the function.

At any rate, it sounds like you need to see some complete working code...

0
Middle School Assistant TeacherCommented:
(hit the submit button on accident)

Here is a very simple app that uses the function provided by Arthur_Wood:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(8, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(128, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "Enter an Integer Here"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(144, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "DivideNumber()"
'
'Label1
'
Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.Label1.Location = New System.Drawing.Point(248, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(144, 24)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Result"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(400, 38)
Me.Name = "Form1"
Me.Text = "Example of Divide by 2 Function"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim value As Integer
Try
value = Integer.Parse(TextBox1.Text)
Label1.Text = DivideNumber(value).ToString
Catch ex As Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Function DivideNumber(ByVal Number As Integer) As Double
Return Number / 2
End Function

End Class
0
Author Commented:
thank you guys... it is working perfect!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.