Results 1 to 6 of 6

Thread: Help With Functions Project

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Help With Functions Project

    Hi im trying to do this project for class on functions. I am little confused on what i am doing wrong.

    Here is the assignment

    1) Write a program to calculate the wind chill. The wind chill is calculated by using the ambient temperature and the wind speed.

    Wind chill temp = 91.4 + (temp - 91.4)(.474 + .304 sqrt(Wind Speed) - .0203(wind speed))

    2) write a program to convert a numerical grade to a letter grade.

    below 70 -F
    70-79 - C
    80-89 - B
    90-100 - A

    Here is the form

    Name:  15e0923954761c3d902a7985c6ba0389.jpg
Views: 268
Size:  16.0 KB


    Here is the code


    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim IntTemp, IntWS As Integer
            IntTemp = Val(TextBox1.Text)
            IntWS = Val(TextBox2.Text)
            Label1.Text = WindChill(IntTemp, IntWS)
        End Sub
            Function WindChill(ByVal Temp As Double, ByVal Wspeed As Double) As Double
            Return 91.4 + (Temp - 91.4)(0.474 + 0.304Sqrt(Wspeed) - 0.0203(Wspeed))
        End Function
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim grade As Integer
            grade = TextBox1.Text
            Label3.Text = Lettergrade(grade)
        End Sub
    
        Function Lettergrade(ByVal Numgrade As Integer) As Char
            If Numgrade < 70 Then
                Label3.Text = "F"
            ElseIf Numgrade < 79 Then
                Label3.Text = "C"
            ElseIf Numgrade < 89 Then
                Label3.Text = "B"
            ElseIf Numgrade < 100 Then
                Label3.Text = "A"
            End If
        End Function
    
    
    End Class

    Thanks for the help. I know there is something wrong with my code, just dont know how to fix it.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Help With Functions Project

    You can't copy the equation directly as stated. You have to convert it to code.
    0.203(Wspeed) is multiplication in the instructions, but it is not multiplication in code.
    0.203 * Wspeed is multiplication in code. You need the * operator for multiplication.
    (x - y) (a + b) indicates multiplication of the two expressions in the instructions, but is not multiplication in code.
    (x - y) * (a + b) To multiply the two expressions.

    Sqrt is a method (function) of the Math class. You have to either say Math.Sqrt(x) or do an Imports Math at the top of the file to bring the Math namespace into scope so you don't have to put Math.xxxx on every function.
    Last edited by passel; Apr 28th, 2015 at 03:39 PM.

  3. #3
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Help With Functions Project

    Have you tried stepping through your code in debug mode one line at a time?
    doing so and examining each variable should show you the problem.

    Ah! you slipped in there Passel!
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Help With Functions Project

    Yeah. He can't step through the code until he can compile it.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Re: Help With Functions Project

    Thanks i fixed the first assignment!!

    I still cant get the 2nd assignment. When i try to debug it says Function lettergrade doesnt return a value on all code paths. Are you missing a return statement?

    I dont know what that means.

  6. #6
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: Help With Functions Project

    There are two types of procedures in VB. Sub Routines and Functions.
    Functions should return a single value or object. Sub Routines do not.

    In VB.NET you should use the 'Returns' keyword with a value you want returned.
    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      Dim Answer as integer = 21
      Answer = AddTwo(Answer)
      MessageBox.Show(Answer.Tostring)
      'Will show 23.
    End Sub
    
    Public Function AddTwo(Input as integer) as Integer
      Dim Total as integer = Input + 2
      Return Total
    End Function
    In short you are mis-using a function.
    you are not returning anything from inside your function.
    Last edited by Gruff; Apr 28th, 2015 at 05:41 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width