# Basic Question about Passing Values between Functions

• 05-08-2012
beeson76
Basic Question about Passing Values between Functions
Here is a basic program that computes square feet of a house. It ask how many rooms...then ask for the width and length of each room, and then totals square foot from that. I wrote this program myself, and can do it without functions. But I am working on functions, and I cannot get the values passed right. I just don't really understand how to pass values yet I guess:) I want to pass the value of rooms between 2 functions named HOWMANYROOMS and SQUAREFEET. The program just dies after I enter the rooms (it goes back to dos prompt. I am using the Dos Prompt Command from Visual Studio 2010 by the way. Here is the program. I want to keep is as basic as possible for right now:)...there are probably better more efficient ways of programming it, but I want to just keep it very basic to learn the basics.

Thanks for any and all help received. I appreciate it very much.

Code:

#include <stdio.h>

int howmanyrooms(int rooms);
int squarefoot(int totalsquare, int rooms);

int main(void)

{

int totalsquare = 0;
int rooms = 0;

printf("This program computes square footage of a house.\n");

howmanyrooms(rooms);

squarefoot(totalsquare, rooms);

printf("Total Square Foot is %d", totalsquare);

return 0;

}

int howmanyrooms(int rooms)
{
printf("How many rooms are in this house?\n");
scanf("%d", &rooms);

return rooms;
}

int squarefoot(int totalsquare, int rooms)
{

int roomnumber, width, length, roomsquare, i;

roomnumber = 1;

for (i = 0; i < rooms; ++i)
{
printf("What are the width of room %d\n", roomnumber);
scanf("%d", &width);
printf("What is the length of room %d\n", roomnumber);
scanf("%d", &length);

roomsquare = width * length;

totalsquare = totalsquare + roomsquare;

++roomnumber;
}

}

• 05-08-2012
dmh2000
you aren't getting the modified 'rooms' back from howmanyrooms so it is always 0. when you pass in a value to a function, it becomes local to the function and when you modify the parameter inside the function, it has no effect on the callers data.

you have two choices,
1. use the return value of howmanyrooms. you don't even need to pass in a value to it (edit : you would need to change your declaration and function to howmanrooms(void) and read in a local variable and return it)
Code:

rooms = howmanyrooms();
2. pass in a POINTER to 'rooms' so you can modify the value.
Code:

declare:    int howmanyrooms(int *rooms){..}
call:        howmanyrooms(&rooms);

i think #1 would be the preferred method and your function is already set up properly to do that.
• 05-08-2012
beeson76
Thanks dmh2000 for the quick reply. I appreciate it very much. I did number 1 and it works perfectly now. I spent a couple hours trying different ways...and to to no avail. I figured that was my problem was not getting an modified value for rooms. I just figured I was doing it by returning rooms. I guess I was wrong:) Here is another question. For something like this, is there any way of making rooms a local variable and passing the value to main()...is that what using Pointers do. I havent gotten to pointers yet:)
• 05-08-2012
rpet
Just do it:

Code:

int howmanyrooms()
{
int rooms; /* local variable */

...

return rooms;
}

The same with "squarefoot":

Code:

int squarefoot(int rooms) /* no need to pass totalsquare */
{
int totalsquare;

...

}

• 05-08-2012
claudiu
Pointers are data types which store memory addresses. Using the second method dmh posted, you can declare your variable in main, pass a pointer to it(i.e. its address) to your function and then manipulate it(the same one from main, not a local copy) in your function. That way your function will write at the precise memory location of where that main variable stores its data, therefore changes will be reflected after control is returned back to main.
• 05-08-2012
koplersky
I don't know if I understood your question, but I'll try to help.

Suppose you want a function that returns the sum of two integers passed to it. It's going to be something like this:
Code:

int sum (int a, int b)
{
int s = a + b;

return s;
}

You see, you need to pass only two integers to that function, which are those being added. And then, to use the sum within main, you need to have a variable to store the returned value s.

To do that, you create a variable and set its value to the returned value of the function. Like this:
Code:

num1 = 2; //number 1 to be added
num2 = 4; //number 2 to be added

int k; //variable that will store the returned value
k = sum (num1, num2); //k will be now the value returned
//from the function sum, in this case  (2+4)=6

So, in this case, the variable s in the function sum is local, because it's declared within the function and thus can only be used there.

If you want a variable from main to store the value of s, you need to do like here, return this value from the function and store it in the desired variable by equalling it to the call procedure.

Hmm, I don't know if I made myself clear or if I said anything wrong, if so please someone point it out. Hope that helps.
• 05-08-2012
beeson76
Thanks for the replies. I am reading everyone and understanding perfectly. Here is a function that passes too values (ANSWERS function). I know I should comment out, but it gets too unorganized in notepad with the editing. This function just adds two similar numbers together and tells you if you are right or wrong. It keeps track of all the right and wrong answers and tells you how many you got of each. I ask what you want to add too: for example if you pick 3, it will add 1+1, 2+2, 3+3 and tells you how many you got right and wrong. It works perfectly until you get to how many you got right and wrong, and it tells you 0. I know the problem is passing the values of right and wrong back to the main(). It isnt modifying the values. So I guess my question would be is: how do you pass 2 values back. Thanks again for the help. Im learning a lot here:)

Code:

#include <stdio.h>

int topnumber(void);
int answers(int number, int right, int wrong);

int main(void)
{

int right = 0, wrong = 0, number, answer;

printf("This is an addition drill program.\n");

number = topnumber();

printf("You got %d Right!...and %d Wrong!", right, wrong);

return 0;

}

int topnumber(number)
{
printf("What number do you want to add to? ");
scanf("%d", &number);
return number;
}
int answers(int number, int wrong, int right)
{

for (i = number; i > 0; i--)
{
printf("What is %d + %d\n", i, i);

{
printf("Right!\n");
right++;
}
else
{
printf("Wrong!\n");
wrong++;
}
}
return right, wrong;
}

• 05-08-2012
dwks
In C++ you could use references, but since this is C, in order to return multiple values you basically have to use pointers. (Using so-called "pass-by-reference".) dmh2000 already mentioned this but here's a quick example:
Code:

#include <stdio.h>

void returnTwoValues(int *x, int *y) {
*x = 3/* this syntax says, dereference the pointer x and go to where it's really pointing */
*y = 4;
}

int main() {
int a, b;  /* notice that the names here don't have to match the names of parameters */
returnTwoValues(&a, &b)/* the & syntax says, take the address of this variable, rather than the value it stores */
printf("%d %d\n", a, b);
return 0;
}

• 05-08-2012
claudiu
It is worth noting that C does not have a "pass-by-reference" per se, pointers are also considered values, and the values they store are memory addresses. So even when you are passing a pointer to a function you are still passing a value.
• 05-08-2012
beeson76
Thanks for the replie claudiu and dwks.

I think I have the program like dwks's example. It is still doing the same thing (it isnt modifying the values of right and wrong). So I must still be doing it wrong:) Any help...please:)

Code:

#include <stdio.h>

int topnumber(void);
int answers(int number, int *right, int *wrong);

int main(void)
{

int right = 0, wrong = 0, number, answer;

printf("This is an addition drill program.\n");

number = topnumber();

printf("You got %d Right!...and %d Wrong!", right, wrong);

return 0;

}

int topnumber(number)
{
printf("What number do you want to add to? ");
scanf("%d", &number);
return number;
}
int answers(int number, int *right, int *wrong)
{

for (i = number; i > 0; i--)
{
printf("What is %d + %d\n", i, i);

{
printf("Right!\n");
*right++;
}
else
{
printf("Wrong!\n");
*wrong++;
}
}
return 0;
}

• 05-08-2012
ZuK
Code:

*right++;
Increments the pointer

you want to increment the value the poiner points to
Code:

(*right)++;
Kurt