I have to separate a program I created into 1 main function and 3 user defined functions.
The instructions for my work is as follows:
// gets an integer from the user and returns it // make 3 calls to
this function: // get the length of the rectangle from the user and
return it to main // get the width of the rectangle from the user and
return it to main // get the radius of the circle from the user and
return it to main int GetNum(void);// takes two arguments, the length and width of the rectangle and
returns the area int CalculateAreaR(int length, int width);// takes one argument, the radius of the circle and returns the area
double CalculateAreaC(int radius);
I am pretty stuck though. I’ve written the functions but can’t correctly call them to the main function. I know it may be simple but there is just something im not seeing right. the code I have written is as follows:
#include <stdio.h>
#include <math.h>
#define PI 3.14
int GetNum(void)
{
int length;
int width;
int radius;
printf( " Please enter the length of a rectangle \n");
scanf(" %d", &length);
printf(" Please enter the width of a rectangle \n");
scanf(" %d", &width);
printf(" Please enter the radius of a circle \n");
scanf(" %d", &radius);
return length, width, radius;
}
int CalculateAreaR(int length, int width)
{
return length*width;
}
double CalculateAreaC(int radius)
{
return PI*radius*radius;
}
int main(void)
{
int length;
int width;
int radius;
int areaR;
double areaC;
GetNum();
printf("\nThe area of the rectangle is %d\n", CalculateAreaR);
printf("\nThe length is %d, the width is, %d and thus the area of the rectangle is %d\n\n", length, width, areaR);
areaC = CalculateAreaC();
printf("\nThe area of the circle is %.3f\n", CalculateAreaC);
printf("\n\n The radius of the circle is %d and the area of the circle is %.3f\n\n", radius, areaC);
return 0;
}
Can anyone please help me? I’d be very thankful. Im trying my best to learn.
In C, a function can only return a single value via the
returnstatement.For a simple program, you could make your
GetNum()function modify global variables.This shows declaring the variables at global scope, then using them in the function.
A more advanced, but usually better, way to return multiple values is for the caller to pass a pointer to a variable, and the function to use the pointer to set the variable. @bash0r showed this technique in his/her answer.
Now, to call a function, you must always put parentheses after the function name. Always always always. If you put the name without the parentheses, you are not calling the function; you are just referring to the function (you are referencing the address of the function). You have a couple of places where you wanted to call functions but you didn’t put the parentheses.
Some functions take arguments. When you call a function that takes arguments, you need to pass the arguments in. Here is an example of a function that multiplies a number by a factor and then adds a term.
In the example, we pass in
input_valueandscale_factor. We don’t have a constant or variable with a term to add, so we just pass in a literal0.0value. So for this example, we are passing all the required arguments. Then the function returns a value, and we collect that output value in the variableadjusted.If you do try the global variables as I suggested, you will need to delete the lines that declare the variables inside the
main()function. Otherwise you will declare two sets of variables, the ones private inside ofmain()and the other, global ones. The ones inmain()will sort of hide the global ones; we call that “shadowing”, as in “the local variables inside ofmain()are shadowing the global variables.”Good luck.