Basic Question about Passing Values between Functions

This is a discussion on Basic Question about Passing Values between Functions within the C Programming forums, part of the General Programming Boards category; Here is a basic program that computes square feet of a house. It ask how many rooms...then ask for the ...

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.

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.

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

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.

1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

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

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:

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

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.

1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.