ICS2O1 Functions VB Str$, Val, Int, Sqr, Abs

ICS2O1 Functions VB Str$, Val, Int, Sqr, Abs


Good morning. This is functions.
Please make sure that you have the handout in front of you. If you don’t
have it go and get it. This is what the two handouts look like. We’re going to
go through five different functions that Visual Basic provides. Those five
functions allow you to do a variety of tasks very quickly. The first one that
I’m going to show you is Str$ What Str$ does is it takes a number and it turns it into a string. You’ll
notice that I’ve also got two green boxes. The first one:
Integers, singles, doubles, longs are all automatically converted to strings when
they’re concatenated. Remember how we just learned about the ampersand? When
you use the ampersand with a numeric value it will automatically convert it
to a string. Str$ tells the program I want you
to convert this number to a string. Sometimes you need it, sometimes
you don’t. The word we use is explicit. That means you are giving a
specific instruction. Take a look at the code snippet that I’ve got here.
‘X is assigned 5″. Then I’m going to
create my message (Msg) and to Msg I assign the answer is which is a string.
Then I turn my number into a string. Display the message.
You have not yet learned how to use MsgBox but it’s a very easy way of
showing a message; a string output to the user. The second function is called Val.
Val extracts the value of a string and it returns the numeric
value. If string contains a number it will give you a numeric value. in the green box notice Val. Val strips away all spaces
and tabs. If you enter a space into a string Val ignores it. Val also stops
reading the string as soon as it it finds a character that it cannot
recognize as a number. The letter G is not part of any decimal numbering system
so G would have to be rejected. On the right
we’ve got three points the first is that Val turns a string into a number. The
second is if you give Val a string that isn’t a number what it gives you is 0.
This is a strength and a weakness. What it means is that you can’t rely on Val
telling you that the string is not a number. We actually have a specific
function that will test to see whether a string is a number but we don’t
introduce you to that yet. We’re sticking with Val. This
works. What I’m trying to tell you is you can’t be sure if your string is 0 or
if your string isn’t a number. That’s okay. Finally,
remember that E-notation is considered numeric. Val is capable
of taking E-notation and converting it into a numeric value. I have a
number of examples. They match up with your sheet. In the case of Val(645.23). That is pretty straightforward it
will convert to 645.23. The next one Val(“22 Acacia”). Val recognizes 22
and then it stops. It sees the space and then
it stops at the ‘a’ where it can’t figure out what’s going on. You end up with
22. Val(“Seven”). What do you think this is gonna be? 0.
It obviously is not anything numeric The Val(“198 43rd Street”).
I’d like you to take a moment and try to predict what is going
to come out. It ignores the spaces so it strips the leading spaces
and it ignores the space between 198 and 43 to give you 19,843. The next one.
the Val of an empty string is 0. Val(“0”) is 0 The Val of
729 East 49th Street. What do we think that’s going to be? The “E” counts as
‘to the power of 10’. It is going to be the E-notation. This actually would give you a very large number. And the
last one 729 is what you would get because W is not part of E-notation.
The third function function involves a number line and it
is Int(). it’s an interesting function. What it does is it returns the largest
integer less than or equal to the number. One thing that will catch people
off-guard is negative numbers. Pay close attention to the examples on the next
slide for the negative numbers. I’ve got my examples. I’ve got a number line here at the top and the examples on the side. First one. Int(4.2). You’ll notice the circle
that appeared on the number line. That represents 4.2. When we apply the
function Int() to it we end up with the value 4. Notice how it goes back to the
largest integer less than or equal to 4.2. 3.7. It’s appeared on the number line and notice
what’s going to happen when we apply Int() to it. We end up with 3. 3.7 goes back
to the largest integer less than or equal to it. 0.97. Very close to 1. If we were rounding it would round up to 1 but
we’re not rounding we are applying Int(). It goes down to 0. Next, -3.6. This is what catches people off guard if
they don’t think about the number line. You can follow closely what’s going to
happen. It’s going to go down to -4. It makes perfect sense when you look
at the number line. It is not toward 0, it’s towards the next
largest integer less than or equal to it. -1.2 has appeared on the number line.
It will go down to -2. We’ve got some additional examples.
18.99999. will give you 18. -99.7
becomes negative 100. Think of it always going to the left on
the number line. The fourth function is finding the square root
of a number. Quite straightforward. Sqr() of a number
returns the square root of a number. There is only one restriction: you can’t
have a negative number. We have three examples. The square root of 49 is 7. The square root of 27.04 is 5.2. The square root of -25… Error! The final
function is the absolute number function. It returns the absolute value of the
number given to it and in case you’ve forgotten what an absolute number is
it’s the magnitude of a real number without regard to its sign. What
exactly is absolute? It’s merely the numeric value without the sign. For
example Abs(9) is 9. The absolute value of -5 is 5 because we
ditched the negative sign. The absolute value of 0 is 0. Here you have the three
possible inputs. There is nothing else inside the real number system that you
can give to Abs(). That’s it. We only need three examples to cover all bases so the
last thing we’re going to do is combine functions. The first function takes the
square root of the absolute value of these squares so we’ve got a
difference of squares and our difference of squares is 64 subtract
100 is negative 36. W e’re not concerned about the negative 36 because it becomes
positive 36 through the Abs() function then we can take the square root of 36
to give us 6. Here you can see that you can combine multiple functions to
accomplish something. A second example. You will also see this on an
evaluation at some point. In fact, just so you know, you won’t be getting
calculators on your tests. You WILL get this kind of question on
your test and you cannot use a calculator. The reason for that is I
don’t need to know decimals. We take the Int() of the square root of 79. You ask
“how can we possibly do that?” First of all it’s going to be the Int() of eight
point something. I don’t exactly know what the decimal is. I don’t care what
the decimal is because the Int() takes my value and it returns the next highest
integer that is less than the value so I get eight. Now let’s take a look at the details of that
particular question. You know a few things. The first thing you know is that
the square root of 64 will be less than the square root of 79 which will be less
than the square root of 81 you also know that the square root of 64 is 8 by now I
hope you’ve learned your basic square roots you also know that the square root
of 81 is 9 so you can infer that the square root of knit 79 has to be greater
than 8 and it has to be less than 9 greater than 8 less than 9 if you apply
int to that you will get 8 yes on a test would you have to show all of this blue
writing no you would not I would basically expect you to write what you
see here in black that would be as much as I need what I would recommend if you
don’t already know them is to memorize the first five or six more advanced
square roots so I presume that everybody knows square root of 1 square root of 4
square root of 9 square root of 16 take a moment memorize these if you
haven’t now this is the end of functions at this point you will get practice
sheets for functions for the most part you shouldn’t run into problems the one
thing that I do see causing people confusion is the int function so do pay
attention to all of the questions that relate to int especially if you find
working outside of a number line confusing so to try to focus on how to
use the number line to solve int type

Leave a Reply

Your email address will not be published. Required fields are marked *