# Function Procedures

Page 1 of 1

## 5 Replies - 935 Views - Last Post: 16 July 2010 - 03:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=181853&amp;s=734e2aee6efe675f3b83b10e562b8dcb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Dave247

Reputation: 0
• 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 number As Integer

Integer.TryParse(inputTextBox.Text, number)

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

• D.I.C Lover

Reputation: 415
• Posts: 2,054
• 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

### #3 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5000
• Posts: 9,055
• 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.

### #4 Dave247

Reputation: 0
• 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.

### #5 rgfirefly24

• D.I.C Lover

Reputation: 415
• Posts: 2,054
• Joined: 07-April 08

## Re: Function Procedures

Posted 16 July 2010 - 01:52 PM

that is correct

• MrCupOfT

Reputation: 2294
• Posts: 9,531
• Joined: 29-May 08

## Re: Function Procedures

Posted 16 July 2010 - 03:52 PM

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