5 Replies - 709 Views - Last Post: 16 July 2010 - 03:52 PM Rate Topic: -----

#1 Dave247  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-January 10

Function Procedures

Posted 16 July 2010 - 12:56 PM

Hi guys

Have come across something while learning about Sub and function procedures that has left me a bit confused, and was hoping someone would steer me in the right direction. I am currently studying from a textbook that sets small exercises at the end of each chapter to test you on the knowledge you have just acquired. The latest exercise asks you to write code for: A function procedure that receives the value stored in an integer variable, The procedure named (Divide Number) should divide the integer by 2 and return the result (which may contain a decimal place). The name of the integer variable passed to the function is (number) and the value returned by the function needs to be passed to a variable called answer. I completed the code, and was having a problem converting the integer data type to decimal. Being inquisitive i changed the datatype of the function to decimal and it worked, this is what left me confused, I thought the data type of the variable being passed to the function had to match the datatype declared in the function or is this only true when working with sub procedures. scratching my head on this one lol!! Here's my code:-

Public Class DivideNumFunction

    Private Sub divideButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles divideButton.Click
        Dim answer As Decimal
        Dim number As Integer

        Integer.TryParse(inputTextBox.Text, number)

        answer = DivideNumber(number)

        resultLabel.Text = answer.ToString("N2")
    End Sub

    Private Function DivideNumber(ByVal num As Decimal) As Decimal
        Dim division As Decimal

        division = num / 2
        Return division

    End Function

End Class


Is This A Good Question/Topic? 0
  • +

Replies To: Function Procedures

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: Function Procedures

Posted 16 July 2010 - 01:24 PM

Your input only tells you what is going to be passed to function or sub.

EX: i could have Public Function a (ByVal b as String) As Int

What that is saying is that the Function a takes an argument of type string. It then returns a value of type Int. How you achieve that is up to you.

So according to your book you need to create a function which takes an argument of type integer, and returns a value of type Decimal

So your decliration would look like this:
Private Function DivideNumber(ByVal number as Integer) As Decimal 



Now within your function you will need to change the Integer to a Decimal. I would recomend using Decimal.TryParse()or Convert.ToDecimal()

This post has been edited by rgfirefly24: 16 July 2010 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Function Procedures

Posted 16 July 2010 - 01:37 PM

Parse and TryParse are for converting strings into the desired data type. Convert.To is the proper way to do this.
Was This Post Helpful? 0
  • +
  • -

#4 Dave247  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-January 10

Re: Function Procedures

Posted 16 July 2010 - 01:48 PM

Thanks man!!

So am i right in thinking that the first part of the function procedure header i.e

Private Function(ByVal Num as integer)


is passing the value of what is stored in the variable (number) to the newly created variable (Num) and the datatype of integer should be the same,
and the last part i.e

As decimal


is the datatype you are going to pass back once the variable is converted in the function.
Was This Post Helpful? 0
  • +
  • -

#5 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: Function Procedures

Posted 16 July 2010 - 01:52 PM

that is correct
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Function Procedures

Posted 16 July 2010 - 03:52 PM

View PostDave247, on 16 July 2010 - 07:56 PM, said:

Being inquisitive i changed the datatype of the function to decimal and it worked, this is what left me confused, I thought the data type of the variable being passed to the function had to match the datatype declared in the function or is this only true when working with sub procedures. scratching my head on this one lol!!


This is true if Option Strict is on. (Most case its turned off by default.)
It has thrown you because most the base-type number classes like integer, double and single. For example and integer can considered to be a double without any digits after the decimal point. But be aware it may not true of a values.
A Double be not an integer, either because it is out the range an integer can be or isn't a whole number.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1