- Still under the topic of function,
let's examine a little bit
in further detail the components of a function.
First of all, there is a declaration of a function,
where you define the header, the name, and so on.
Then you think of, 'Does the function return a type?'
Maybe it doesn't.
If it does, what type of value should it return?
What is the most appropriate name for this function?
Should we just call it "function?"
"Function 1?"
Or should we call it "average of 2 numbers?"
Parameters: does the function require
any external input?
Does it require parameters
that are numbers, that are text?
You have to be aligned with what
exactly is expected from the problem
that this function is trying to solve.
Finally, the body of the function,
"the statements that make up the function."
Obviously the function has to do something
so before you write the function,
just ask yourself,
do you have an idea
of how you're going to go about this function?
I have said that many times throughout this course.
Think, and plan,
and never rush into just writing code.
A simple function,
is a great way to examine some of the things
in detail because even in simpler functions,
sometimes there are things that are worth noticing,
and sometimes they can get lost.
For example,
this function is meant to compare two numbers,
and return the smallest of the two.
Obviously, because of that,
it requires external data
to be passed as arguments
to its two "int" parameters, "x" and "y."
At the heart of the function,
the main idea was,
we are going to compare those two values,
and we will find whichever is the smallest,
and this will be the return value.
So this function receives two inputs,
and returns one.
Which is one of those inputs.
It makes a selection.
Therefore,
after we decided that
the core of the function is this,
things like the second line of the function
"int minVal,"
this is just a side effect of us deciding
that the function would first compare
those two numbers.
So this is the heart of the function.
That's the main idea.
How we going about implementing it,
let's stacks it.
Sometimes you need one, or two, or three variables.
They're called 'helper variables,'
or 'temporary variables.'
And so on.
This is not necessarily supposed to be
the best, most efficient
the most awesomest function
for comparing two numbers.
But, it works,
as we'll see shortly in a C program.
So, here's the function.
Just like as we saw.
And...
We are now going to compare
two values "int" x and y.
Now, notice that the values I am passing
to the function are not numbers.
They are two characters.
Let's run the code and let's see
if there's a problem, and then we'll focus
on solving the issues.
But I want to run the code first
so we'll have something at hand to talk about.
Okay, we see "n=97."
What could possibly "97" come from?
The function was called, in line 18,
"smallest('r' , 'a')" so naturally one would expect
to receive an answer which one is smaller.
The letter 'r,' or the letter 'a?'
What does it mean?
If you examine the function,
there is nothing here to suggest
that the function is capable
of comparing two letters.
Unless you know that the function
might be able to translate
a character into its ASCII code,
because after all, the language C can do stuff like that.
So let's examine this a little further.
These are some of the questions we have to ask ourselves
before we implement the function
so that we don't' come up across a situation
where we're puzzled.
We see that line 18
we're calling the function with two characters
yet the function returns a number.
I have no idea what to do with this result.
So let's look at line 26
where I put the comment deliberately here
and not outside this document
because it's very important
when you think about a function,
and when you think about coding,
to have those tips and questions in front of you.
"Is the return type same as the function data type?"
For example,
If the function returns "minVal," which is "int,"
has it been set so that the data type
is also an "int?" In this case, yes.
"Is the function name appropriate for what it does?"
"smallest," I'm not sure yet what the function does,
but that's one of the questions we have to ask.
"Are the parameters correctly typed?"
Does this function work on integers?
Is it supposed to work on integers?
Is the input that this function
is supposed to process integer?
Line 29, "Is the number of parameters
"in agreement with what the function input
"is supposed to be?"
Was I supposed to design a function
that receives two external values,
maybe three, maybe five,
maybe an arbitrary number?
"Is the function working properly on all inputs?"
Does it work with negative numbers?
Does it work with identical number?
What if the input is not an "int?"
Is typecasting an option?
Typecasting means we are changing the type
of a variable for a specific operation
so that the operation can be carried on
between compatible types.
If you can run the function
with two float arguments,
what does this mean?
You can run the function with two character arguments.
What does that mean?
Obviously, if I'm going to change the function,
instead of 'r' and 'a,'
I'm going to choose '5,'
and '12,'
and I'm going to save,
and run the function.
The "smallest" between '5' and '12' is 5,
so I know that the function works correctly
on integer numbers.
What about floating point numbers?
Will it still work?
Let's see.
It still works, however,
the result is an integer.
What if I had something like this?
And...
"11.99"
Would the function still work in this case?
I still get the result "5."
What if it was
"11.98"
against "11.99?"
Will the function still work?
The answer is "11," inconclusive.
So this is an example
of a thorough testing you have to perform
if and when we are discussing
a very small, very seemingly deceivingly
simple function.
Imagine the length of testing
you will have to go through
for much complex programs.