## Friday, September 23, 2011

### Coding Lessons: C and functions (lesson 7)

OK so we learned a little about accepting inputs, and made a sort of useful tool into the bargain. -I say sort of useful because there are online versions everywhere, the point wasn't to create a useful tool, the point was to introduce manipulating the variables.

Now lets take a step back and look at some simple functions.

we always have a main function, this is where the "meat" of our program goes.

but lets says that we have a defined function

we'll take something simple, like A+B
now we can write C = A+B.

but what if this were actually a really complicated equation, and we're using it hundreds of times, are we going to write it out each time? copy and paste code?

what if we notice a mistake -then we'll hace to correct all the hundreds of times we've written this out.

I guess it's not easier to write c = add(a, b) but for a more complex function this is useful.
what if it weren't adding, what if it were calculating VAT, a change in rate means that you have to search all your code, but if you had a function for calculating VAT, you'd only need to change that function.

Lets' look again at BMI.

as before we include standard libraries:
#include <stdio.h>but this time there is a change, we don't jump right into our main function, we tell the program that there is another function.

float bodymassindex(h, w);

Everything in the first half of this example is the same as the last lesson, show prompts, gather data.
int main()
{
int weight, height;
float bmi;
printf("BMI Calculator\r\n");
scanf("%d", &weight);
scanf("%d", &height);
But in this example the data processing has been moved out to a function, we pass that function the numbers,
bmi = bodymassindex(height, weight); it returns a result, which we then carry on using as normal.
printf("\r\n\r\nUnderweight = <18 -="-" .5=".5" 24.9="24.9" 29.9="29.9" 30="30" br="br" greater="greater" n="n" nnormal="nnormal" nobesity="BMI" noverweight="25" of="of" or="or" r="r" weight="18.5">}
the function is written underneath the main part of the program, but in reality it works just the same as the program.
in the main program we expect an integer error code to say if execution has completed sucessfully, so the main part of the program is declared as int main()
We're expecting a floating point to be returned, so we declare the function as a float.
float bodymassindex(int h, int w)
also we tell the function what sort of variables it'll be getting, notice that they have to be the same type (in this case integers), but they don't need the same names, so I shortened height to h and weight to w.

then we come to the function, just as in the last example this calculates the BMI number
{
float result;
/*bmi = mass(kg) / height^2(m)*/
result = h * h;
result = result/10000;
result = w/result;
Then the function returns it's result using the return function.
return(result);
}

Put all together the code looks like this: (and gives the same output as the last lesson).

#include <stdio.h>
float bodymassindex(h, w);
int main()
{
int weight, height;
float bmi;
printf("BMI Calculator\r\n");
scanf("%d", &weight);
scanf("%d", &height);
bmi = bodymassindex(height, weight);
printf("\r\n\r\nUnderweight = <18 -="-" .5=".5" 24.9="24.9" 29.9="29.9" 30="30" greater="greater" n="n" nnormal="nnormal" nobesity="BMI" noverweight="25" of="of" or="or" r="r" span="span" weight="18.5">
}

float bodymassindex(int h, int w)
{
float result;
/*bmi = mass(kg) / height^2(m)*/
result = h * h;
result = result/10000;
result = w/result;
return(result);
}

The difference is that this code is much more maintainable, you might not see it for a small program like this, (in fact it's arguable more work to put in functions than just write the calculations in the code), but if this were a part of a much larger, millions of lines of code program, using this function technique means that you can correct errors without having to search through all lines of code.

It can also reduce lines of code, say I was doing this calculation 3 million times, the calculation is only 3 lines long, but that means that over all this will take 3 million lines of code in my source.

if I decided that I needed height cubed instead of squared, I'd have to alter 1 million lines.

using a function I only need to call the function 1 million times so instead of three million lines of code, I have one million, plus the 9 lines involved in setting up and actually doing the function.

now if I decide that I want height cubed instead of squared, I change 1 line of code.