Thread: Basic Question about Passing Values between Functions

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    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;
    }
    
    
    return totalsquare;
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    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.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    4
    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

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    62
    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;
    
        ...
    
        return totalsquare;
    }

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58

    Smile

    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.
    Last edited by koplersky; 05-08-2012 at 10:47 AM.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    4
    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();
    
    
    answer = answers(number, right, wrong);
     
    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)
    {
    
    
        int i, answer;
    
    
        for (i = number; i > 0; i--)
        {
            printf("What is %d + %d\n", i, i);
            scanf("%d", &answer);
    
    
            if(answer == i + i)
                {
                    printf("Right!\n");
                    right++;
                }
            else
                {
                    printf("Wrong!\n");
                    wrong++;
                }
        }
        return right, wrong;
    }

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "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


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    4
    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();
    
    
    answers(number, &right, &wrong);
     
    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)
    {
    
    
        int i, answer;
    
    
        for (i = number; i > 0; i--)
        {
            printf("What is %d + %d\n", i, i);
            scanf("%d", &answer);
    
    
            if(answer == i + i)
                {
                    printf("Right!\n");
                    *right++;
                }
            else
                {
                    printf("Wrong!\n");
                    *wrong++;
                }
        }
        return 0;
    }

  11. #11
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    *right++;
    Increments the pointer

    you want to increment the value the poiner points to
    Code:
    (*right)++;
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Values From Functions
    By skmightymouse in forum C Programming
    Replies: 21
    Last Post: 03-25-2012, 11:48 AM
  2. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  3. Beginner question: passing values between functions
    By merike in forum C Programming
    Replies: 5
    Last Post: 11-18-2006, 08:39 PM
  4. functions passing values
    By srinurocks in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:49 PM
  5. Passing Values to Functions
    By shad0w in forum C Programming
    Replies: 2
    Last Post: 12-25-2001, 08:28 PM