Hello, and welcome.
We pretty much talked about the most basic concepts of
functions, the basics of them.
There are some more advanced concepts related to functions, like function
pointers and static and automatic static variables, recursion.
But those things are going to be discussed in an advanced class.
Now that we know the basics of functions, I want to provide you with
the challenge so you get familiar with creating and using functions.
So you're going to actually write a number of different functions.
We need to start getting some practice writing these different functions.
And why do we want to write functions? Because it makes our
code more organized, more modular and you can avoid duplication. We
went through all the advantages.
So the purpose of this assignment is to get you familiar with functions,
understand these advantages.
So you're going to write three functions, and you can have all these
functions defined in a single program.
I want you to write a function which finds the greatest common
divisor of two non-negative integer values and returns the result.
So you can create a function name called GDC.
The function should take two integers as parameters.
The algorithm to find the greatest common divisor pretty simple.
You can look online or find any math book.
What you should be noticing in these challenges is that they're
getting more and more difficult.
I'm not going to provide you with every single hint.
You have to come up with the GDC algorithm.
It's very easy though and very common.
It's a common function.
Have this function take, data and have it return data.
Two non-negative integer values it accepts.
You can either check for negative integers if you want or you
don't have to, it's up to you.
I also want you to write a function to calculate the
absolute value of a number.
The absolute value of a number is positive, positive value.
So if you if you have a number negative five, the absolute
value of that is five.
If you have a positive number five, the absolute value is five,
it's a very simple function.
Actually, the implementation of this function all you're
going to need to do is really check if it's less than 0.
And if it is just make sure it's positive.
If it's a variable, you just say x equals negative x.
You'll want to test this function with ints and floats to make sure it works.
To make the absolute value function more usable, I would
have it take a parameter as a float and return to float.
That way you can actually use it with an int and you
won't lose any precision.
If you pass an int in there it's automatically going
to convert it to a float.
And if you had an int, have the parameters an int and returned an int if you tried
to use a float, you would lose data.
So make it take floats.
Third function that you're going to write is going to compute
the square root of a number.
Don't use any helper or any standard library functions.
Do the square root with an algorithm that you create.
I also want you to check the parameters passed to this method.
If you have any values passed to the function and the values are negative,
I want you to display a message and I want you to return a negative one.
What this means is you can actually use the other function you created,
absolute value as a helper function.
That's what you use.
When one function uses another function, it sometimes is
referred to as a helper function.
So the square root function I want you to invoke the absolute value function.
You can use that to test whether or not the arguments are negative.
You can also actually use it when you do your computation
of the square root.
Even though you're returning a negative number, you still
want to call the absolute value function when you're performing
the square root itself.
So these are the three functions I want you to write.
You're going to be exposed to creating -- declaring your functions.
I want you to have function prototypes at the top of the file because
remember that's good convention.
I want you to return the correct data.
I want you to pass data to these functions, so you're going to
have to understand the whole entire function signature.
And then you're going to invoke these functions.
When you when you actually go and start testing the program, make
sure you have a lot of test cases.
Call the GDC function with multiple values.
Call it with negative numbers.
Call the absolute value function with many different values.
You can test all this by just invoking the functions in the main function.
And then you can also have normal output with this.
I'll show you my solution in the next lecture, but think about
this, get a little planning out of the way, try to figure out what
functions are going to return.
Even though I gave you a lot of hints here, what they're going
to return and how they're going to take the data, understand
function declarations and so forth.
Good luck.