locked
I have a question about functions... RRS feed

  • Question

  • Can I use a function for more than one equation?

    Program Description:  Fatuitous Dice is a game that requires two dice be rolled, one at a time.  If p and q are the numbers rolled on the first and second die, respectively the value of the roll in the game is determined as follows:

             P              Q         Rule (value of the roll and condition)
    Even          Odd            2P + Q
    Odd           Even           P + 2Q
    Even          Even           P + Q         When P not = Q
                                            P             When P = Q
    odd           Odd            P + Q         When P not  = Q
                                            3Q              When P = Q

    Can you set an example that is similar to this scenario so that I can easily understand it?

    Tuesday, April 26, 2011 10:08 PM

Answers

  • Remove the MessageBox lines from the function.  The function needs to do one thing only - calculate a result.  The message box should be in the calling routine.   Currently,  your function doesn't return a result.   Change:

            End If
        End Function
    to
            End If
        Return Result
        End Function

    But the function isn't being called! When you call the function, display the result.   Change

            P = Me.txtBoxNumOne.Text
            Q = Me.txtBoxNumTwo.Text
    to
            P = Me.txtBoxNumOne.Text
            Q = Me.txtBoxNumTwo.Text
            MessageBox.Show(DiceRoll(P, Q).ToString)



    • Marked as answer by Kee Poppy Friday, May 6, 2011 3:31 AM
    Wednesday, April 27, 2011 1:44 AM

All replies

  • You could have a function like this:

    Function RollValue(p as Integer, q As Integer) As Integer
    If p Mod 2 = 0 Then
      If q Mod 2 = 0 Then Return If(p = q, p, p+q)
      Return 2 * p + q
    Else
      If q Mod 2 = 0 Then p + 2 * q
      Return if(p = q, 3 * q, p + q)
    End If
    

    Tuesday, April 26, 2011 10:23 PM
  • try this:

     

    Public Class Form1
    
      Dim r As New Random
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dice1 As Integer = r.Next(1, 7)
        Dim dice2 As Integer = r.Next(1, 7)
        Label1.Text = dice1.ToString
        Label2.Text = dice2.ToString
        MsgBox(rollValue(dice1, dice2).ToString)
      End Sub
    
      Private Enum value
        even = 0
        odd = 1
      End Enum
    
      Private Function rollValue(ByVal p As Integer, ByVal q As Integer) As Integer
        Dim v1 As value = DirectCast(p Mod 2, value)
        Dim v2 As value = DirectCast(q Mod 2, value)
    
        If v1 = value.even And v2 = value.odd Then
          Return (p * 2) + q
        ElseIf v1 = value.odd And v2 = value.even Then
          Return p + (2 * q)
        ElseIf v1 = value.even And v2 = value.even Then
          If p <> q Then
            Return p + q
          Else
            Return p
          End If
        ElseIf v1 = value.odd And v2 = value.odd Then
          If p <> q Then
            Return p + q
          Else
            Return 3 * q
          End If
        End If
    
        Return -1
    
      End Function
    
    End Class
    
    

    thanks for any help
    Tuesday, April 26, 2011 10:37 PM
  • Public Class frmGame
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            Dim EvenNum As Double = 0
            Dim OddNum As Double = 0
            Dim EvenNum2 As Double = 0
            Dim OddNum2 As Double = 0
            Dim intValue As String = 0
            Dim NumP As Double = Me.txtBoxNumOne.Text
            Dim NumQ As Double = Me.txtBoxNumTwo.Text
            If NumP Mod 2 = 0 Then
                EvenNum = NumP
            Else
                OddNum = NumP
            End If
            If NumQ Mod 2 = 0 Then
                EvenNum2 = NumQ
            Else
                OddNum2 = NumQ
            End If
            Do
                If EvenNum = NumP And OddNum2 = NumQ Then
                    Me.lblAnswer.Text = (2 * NumP) + NumQ
                End If
                If OddNum = NumP And EvenNum2 = NumQ Then
                    Me.lblAnswer.Text = (2 * NumQ) + NumP
                End If
                If EvenNum = EvenNum2 Then
                    Me.lblAnswer.Text = NumP
                Else
                    Me.lblAnswer.Text = NumP + NumQ
                End If
                If OddNum = OddNum2 Then
                    Me.lblAnswer.Text = NumQ * 3
                Else
                    Me.lblAnswer.Text = NumP + NumQ
                End If
            Loop
            Me.lblAnswer.Text = intValue
        End Sub
        Function add(ByVal intValue)
            Return intValue
        End Function
    End Class
    this is my code for this program and I have yet to figure it out.

    #2: Fatuitous Dice

    Program Description:  Fatuitous Dice is a game that requires two dice be rolled, one at a time.  If p and q are the numbers rolled on the first and second die, respectively the value of the roll in the game is determined as follows:

             P              Q         Rule (value of the roll and condition)
    Even          Odd            2P + Q
    Odd           Even           P + 2Q
    Even          Even           P + Q         When P not = Q
                                            P             When P = Q
    odd           Odd            P + Q         When P not  = Q
                                            3Q              When P = Q

    unit9-Dice-1Write a function "diceroll" that will receive a pair of values P and Q that computes the value of the roll.

    Required Statements:  loop control, decision making, function

     

    Sample output: Design your own output and run your program to validate all conditions.  A sample of correct answers are listed below.  Your output should shown the dice faces of each roll as well as the value according to the game rules.

    You might want to animate the dice as they are being rolled!

    P             Q            Value
    2              5                  9
    4              4                 12
    6              2                  8
    1              3                  4
    5              5                 15
    1              2                  5

    • Merged by Mike Feng Thursday, April 28, 2011 9:54 AM duplicate
    Tuesday, April 26, 2011 10:39 PM
  • You could have a function like this:

     

    Function RollValue(p as Integer, q As Integer) As Integer
    
    If p Mod 2 = 0 Then
    
     If q Mod 2 = 0 Then Return If(p = q, p, p+q)
    
     Return 2 * p + q
    
    Else
    
     If q Mod 2 = 0 Then p + 2 * q
    
     Return if(p = q, 3 * q, p + q)
    
    End If
    
    

     


    you mean:

     

    Function RollValue(ByVal p As Integer, ByVal q As Integer) As Integer
      If p Mod 2 = 0 Then
        If q Mod 2 = 0 Then Return If(p = q, p, p + q)
        Return 2 * p + q
      Else
        If q Mod 2 = 0 Then Return p + (2 * q)
        Return If(p = q, 3 * q, p + q)
      End If
      'ensure return value on all paths
      Return -1
    
    End Function
    

     

    ???


    thanks for any help
    Tuesday, April 26, 2011 10:44 PM
  • I don't think I meant that Paul. I don't believe the Return -1 statement could ever be reached.

    [Edit]

    Sorry, I didn't notice I had left off the End Function. I should certainly have included that.

    Tuesday, April 26, 2011 10:56 PM
  • I don't think I meant that Paul. I don't believe the Return -1 statement could ever be reached.

    [Edit]

    Sorry, I didn't notice I had left off the End Function. I should certainly have included that.


    i fixed an error too...

    your code wouldn't run


    thanks for any help
    Tuesday, April 26, 2011 11:01 PM
  • This code uses functions to determine whether the values are even or odd. It creates a loop to test all combinatins and displays the results in a listbox.

    Public Class Form1

        
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
    Dim Result As Integer
            For I As Integer = 1 To 2
                
    For J As Integer = 1 To 4
                    Result = Roll(I, J)
                    ListBox1.Items.Add(I.ToString & 
    "," & J.ToString & " -> " & Result.ToString)
                
    Next
            Next
        End Sub

        Private Function Roll(ByVal P As IntegerByVal Q As IntegerAs Integer
            Dim R As Integer
            If IsEven(P) AndAlso IsOdd(Q) Then R = (2 * P) + Q
            
    If IsOdd(P) AndAlso IsEven(Q) Then R = P + (2 * Q)
            
    If IsEven(P) AndAlso IsEven(Q) Then
                If (P = Q) Then
                    R = P
                
    Else
                    R = P + Q
                
    End If
            End If
            If IsOdd(P) AndAlso IsOdd(Q) Then
                If (P = Q) Then
                    R = 3 * Q
                
    Else
                    R = P + Q
                
    End If
            End If
            Return R
        
    End Function

        Private Function IsEven(ByVal I As IntegerAs Boolean
            If I Mod 2 = 0 Then
                Return True
            Else
                Return False
            End If
        End Function

        Private Function IsOdd(ByVal I As IntegerAs Boolean
            If I Mod 2 = 1 Then
                Return True
            Else
                Return False
            End If
        End Function

    End
     Class

    Tuesday, April 26, 2011 11:02 PM
  • Ah I see it now, I missed the 3rd Return.  Thanks for correcting that.
    Tuesday, April 26, 2011 11:06 PM
  • You have already determined what changes are needed to that code and asked the correct question here:
    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/d2a06067-108e-4cf5-bc5a-52bf113c9df3

    As there is no point in having two threads about the same problem, you should update your code in the other thread.

    You will generally get better responses to your question if the thread title indicates what the problem is.

     


    Tuesday, April 26, 2011 11:16 PM
  • What is the point of us helping you if it's a training session/homework that is intended to make you learn?

    Public Class Form1
      Dim pdice As Integer
      Dim qdice As Integer
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox3.Text = "" Then
          Return
        ElseIf TextBox4.Text = "" Then
          Return
        End If
        Dim rand As New Random
        pdice = rand.Next(CInt(TextBox3.Text), CInt(TextBox4.Text))
        qdice = rand.Next(CInt(TextBox3.Text), CInt(TextBox4.Text))
        TextBox1.Text = CStr(pdice)
        TextBox2.Text = CStr(qdice)
      End Sub
    
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        pdice = CInt(TextBox1.Text) 'user input textbox1 bypassing generate
        qdice = CInt(TextBox2.Text) 'user input textbox2 bypassing generate
        Dim Result As String = ""
        If pdice Mod 2 = 0 Then
          If qdice Mod 2 = 0 Then     'Both are Even
            If pdice = qdice Then
              Result = CStr(pdice)
            ElseIf pdice <> qdice Then
              Result = CStr(pdice + qdice)
            End If
          ElseIf qdice Mod 2 = 1 Then   'q is odd, p is even
            Result = CStr(qdice + CInt(2 * pdice))
          End If
        ElseIf pdice Mod 2 = 1 Then
          If qdice Mod 2 = 0 Then     'q is even, p is odd
            Result = CStr(pdice + CInt(2 * qdice))
          ElseIf qdice Mod 2 = 1 Then   'Both are Odd
            If pdice = qdice Then
              Result = CStr(3 * qdice)
            ElseIf pdice <> qdice Then
              Result = CStr(qdice + pdice)
            End If
          End If
        End If
        Label5.Text = Result
      End Sub
    End Class
    

    You have to figure out the function diceroll by yourself. It's already been given in the above, but writing functions are nice.

    Why do you have the Function Add? It serves no purpose.


    Sorry if my English is poor, it's not my first language. :) Programming FTW! Samuel
    Tuesday, April 26, 2011 11:42 PM
  • Ok so I wanted to try to figure this out for myself and I got some code so far. I tried it under btnCalculate_ click and it worked but when I tried it as a function it wouldnt put out an answer. 

     

     

     

    Public Class frmGame

        Dim Value As Integer

     

        Dim P As Integer

        Dim Q As Integer

     

        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

            P = Me.txtBoxNumOne.Text

            Q = Me.txtBoxNumTwo.Text

        End Sub

     

        Function DiceRoll(ByVal P As Integer, ByVal Q As Integer) As Integer

            Dim Result As Integer

            If P Mod 2 = 0 And Q Mod 2 = 1 Then

                Result = (2 * P) + Q

                MessageBox.Show(Result)

            End If

            If P Mod 2 = 1 And Q Mod 2 = 0 Then

                Result = (2 * Q) + P

                MessageBox.Show(Result)

            End If

            If P Mod 2 = 0 And Q Mod 2 = 0 And P <> Q Then

                Result = P + Q

                MessageBox.Show(Result)

            End If

            If P Mod 2 = 0 And Q Mod 2 = 0 And P = Q Then

                Result = P

                MessageBox.Show(Result)

            End If

            If P Mod 2 = 1 And Q Mod 2 = 1 And P <> Q Then

                Result = P + Q

                MessageBox.Show(Result)

            End If

            If P Mod 2 = 1 And Q Mod 2 = 1 And P = Q Then

                Result = 3 * Q

                MessageBox.Show(Result)

            End If

        End Function

     

     

     

     

    End Class

     




    • Proposed as answer by Acamar Wednesday, April 27, 2011 1:38 AM
    • Unproposed as answer by Acamar Wednesday, April 27, 2011 1:38 AM
    Wednesday, April 27, 2011 1:25 AM
  • Remove the MessageBox lines from the function.  The function needs to do one thing only - calculate a result.  The message box should be in the calling routine.   Currently,  your function doesn't return a result.   Change:

            End If
        End Function
    to
            End If
        Return Result
        End Function

    But the function isn't being called! When you call the function, display the result.   Change

            P = Me.txtBoxNumOne.Text
            Q = Me.txtBoxNumTwo.Text
    to
            P = Me.txtBoxNumOne.Text
            Q = Me.txtBoxNumTwo.Text
            MessageBox.Show(DiceRoll(P, Q).ToString)



    • Marked as answer by Kee Poppy Friday, May 6, 2011 3:31 AM
    Wednesday, April 27, 2011 1:44 AM