Function procedure

Posted on 2004-10-28
Last Modified: 2010-04-23
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.
Question by:skathari
    LVL 2

    Expert Comment

    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.
    LVL 19

    Expert Comment

    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...

    Author Comment

    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.
    LVL 27

    Expert Comment

    FYI .....

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


    Link for ASP programs...

    LVL 44

    Expert Comment

    in VB.NET this can be wriiten as:

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

    End Function

    LVL 44

    Accepted Solution

    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
    LVL 44

    Expert Comment

    oops, minor type:

    should read:

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

    Author Comment

    but, dont we have to add a button?
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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...

    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    (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()

            'This call is required by the Windows Form Designer.

            '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
                End If
            End If
        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.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"
            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()"
            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
            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"

        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
                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

    Author Comment

    thank you guys... it is working perfect!!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now