# Thread: How to properly use a function?

1. ## How to properly use a function?

Hi guys, so I'm getting myself really confused with functions and I don't really understand them. I am given a simple task which is:

The following program is supposed to calculate and print the area of a triangle. Finish writing the program. You will need to write the areaOfTriangle() function as part of your solution. Make sure you observe the format of the output. In case you forgot, the formula for area of a triangle is -
Code:
area = 1/2 * base * height

...

int main()
{
double base, height, area;
printf("Enter the base and height => ");
scanf(" ? ? ",&base,&height);

area = areaOfTriangle(base,height);

printf("The area is ???\n", area);

...

Sample Output

Enter the base and height => 7.26 6.83
The area is 24.793



Just calculate using a function and then use that function to printf the answer right? I'm not sure how exactly to do that. After many different things I did, I'm just completely stumped. Heres what I have.

Code:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//Prototypes
unsigned areaOfTriangle(base,height);

//Declarations

int main ()
{
printf("Mark Bacani\n");
printf("Lab Ex #6\n");
printf("Code::Blocks\n");

//Local Declarations
double base;
double height;
double area;
//Calculations
printf("Enter the base:\n");
scanf("%lf",&base);
printf("Enter the height:");
scanf("%lf",&height);

area = areaOfTriangle(base,height);

//Result
printf("The area is %lf \n", area);
return 0;
}

//Prototype
unsigned areaOfTriangle (base,height)
{
unsigned areaOfTriangle;

areaOfTriangle =(1/2) * base * height;

return areaOfTriangle;

}
I realize the prototype section is a complete mess. Functions are my kryptonite >

Any help would be greatly appreciated!

2. You might be interested in the messages I got when I compiled your program:
Code:
C:\Users\Josh2\Documents\bar\main.c|7|warning: parameter names (without types) in function declaration [enabled by default]|
C:\Users\Josh2\Documents\bar\main.c||In function 'areaOfTriangle':|
C:\Users\Josh2\Documents\bar\main.c|45|warning: type of 'base' defaults to 'int' [enabled by default]|
C:\Users\Josh2\Documents\bar\main.c|45|warning: type of 'height' defaults to 'int' [enabled by default]|
||=== Build finished: 0 errors, 3 warnings ===|
If that doesn't make much sense, the first number after the file name is the line number. The compiler thinks something is wrong with that line, so if you look the error should be obvious.

In addition to that your formula for area has a problem. (1/2) is 0 because the calculation is done by integers. The decimal point gets truncated. So you will want to fix that by using decimal points instead.

3. Code:
#include <stdio.h>
#include <math.h>

float triangle(float a);

int main()
{
float b,h;
printf("Enter base:\n");
scanf("%f",&b);
printf("Enter height:\n");
scanf("%f",&h);

float a=b*h;

/* 1st call of the function, before finding area.*/
triangle(a);

/* 2nd call of the function, after finding area.*/
printf("The area = %.3f",triangle(a));

return(0);
}

float triangle(float a)
{
float area=a/2;

return(area);
}