Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Function procedure

Posted on 2004-10-28
11
Medium Priority
?
234 Views
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.
0
Comment
Question by:skathari
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 2

Expert Comment

by:dsabo
ID: 12441310
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
 
LVL 19

Expert Comment

by:Fahad Mukhtar
ID: 12441325
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 Comment

by:skathari
ID: 12441351
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:planocz
ID: 12443226
FYI .....
Hi,

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

.....http://msdn.microsoft.com/vbasic/downloads/samples/101samples.aspx

Link for ASP programs...

http://msdn.microsoft.com/asp.net/downloads/kits/default.aspx 

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12443306
in VB.NET this can be wriiten as:

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

End Function


AW
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 400 total points
ID: 12443319
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 12443324
oops, minor type:

should read:

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

Author Comment

by:skathari
ID: 12447695
but, dont we have to add a button?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 12448931
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 400 total points
ID: 12448967
(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.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox1)
        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 Comment

by:skathari
ID: 12450451
thank you guys... it is working perfect!!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

580 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