Possible Duplicate:
How to modify content of the original variable which is passed by value?
I am building a pretty simple program that calculates the area of a rectangle. Simple enough however, as you will notice I cannot seem to get a return value. I keep seeing 0. There is probably an obvious answer or perhaps there is something I just don’t understand. Heres my code:
#include<stdio.h>
//prototypes
int FindArea(int , int , int);
main()
{
//Area of a Rectangle
int rBase,rHeight,rArea = 0;
//get base
printf("\n\n\tThis program will calculate the Area of a rectangle.");
printf("\n\n\tFirst, enter a value of the base of a rectangle:");
scanf(" %d" , &rBase);
//refresh and get height
system("cls");
printf("\n\n\tNow please enter the height of the same rectangle:");
scanf(" %d" , &rHeight);
//refresh and show output
system("cls");
FindArea (rArea , rBase , rHeight);
printf("\n\n\tThe area of this rectangle is %d" , rArea);
getch();
}//end main
int FindArea (rArea , rBase , rHeight)
{
rArea = (rBase * rHeight);
return (rArea);
}//end FindArea
You intialize
rAreato 0. Then, you pass it intoFindAreaby value. This means none of the changes torAreain the function are reflected. You don’t make use of the return value, either. Therefore,rAreastays 0.Option 1 – Use the return value:
Option 2 – Pass by reference: