#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0

    C, function call


    Hello everyone I have this code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int rectangle (int rectangle_length, int rectangle_length2) {
    	int rectangle_length, rectangle_length2;
    	return rectangle_length * rectangle_length2;
    }
    
    int main() {
    int surface=rectangle(rectangle_length, rectangle_length2);
    printf("Enter 2 ribs length of rectangle\n");
    scanf("%d %d", &rectangle_length &rectangle_length2);
    printf("%d\n", surface);
    	system("PAUSE");
    	return 0;
    }
    I have a problem in the function call line, line 10.
    I call the function with the argument.

    What's the problem?
    Very appreciate help. thanks.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int rectangle (int rectangle_length, int rectangle_length2) {
    	//!! you have local variables (uninitialised) with the same name as your parameters
    	//!! guess which takes precedence in your calculation
    	int rectangle_length, rectangle_length2;
    	return rectangle_length * rectangle_length2;
    }
    
    int main() {
    //!! you didn't declare your local variables here
    //!! you're calling the function before you've input the values.
    int surface=rectangle(rectangle_length, rectangle_length2);
    printf("Enter 2 ribs length of rectangle\n");
    //!! you're missing a comma
    scanf("%d %d", &rectangle_length &rectangle_length2);
    printf("%d\n", surface);
    	system("PAUSE");
    	return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Where do you define rectangle_length and rectangle_length2?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    OK, lemme show you the right code that I just wrote:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int rectangle (int rectangle_length, int rectangle_length2) {
    
    	return rectangle_length * rectangle_length2;
    }
    
    
    int main() {
    int x,y;
    int rectangle_surface;
    rectangle_surface=rectangle(x,y);
    printf("Enter 2 ribs length of rectangle\n");
    scanf_s("%d %d", &x, &y);
    printf("%d\n", rectangle_surface);
    	system("PAUSE");
    	return 0;
    }
    Now, the compiler says (Visual Studio 2012) that error:
    PHP Code:
    uninitialized local variable 'y' used 
    PHP Code:
    uninitialized local variable 'x' used 
    What should I do?
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    > rectangle_surface=rectangle(x,y);
    > printf("Enter 2 ribs length of rectangle\n");
    > scanf_s("%d %d", &x, &y);

    You've written
    - drink coffee
    - How many sugars do you want?
    - answer: 2

    Do you understand cause and effect?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by salem
    > rectangle_surface=rectangle(x,y);
    > printf("Enter 2 ribs length of rectangle\n");
    > scanf_s("%d %d", &x, &y);

    You've written
    - drink coffee
    - How many sugars do you want?
    - answer: 2

    Do you understand cause and effect?
    Thank you.
    I still don't understand what you're trying to say me.
    Can you show me the correct code and to write me my errors in it?
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,377
    Rep Power
    1871
    OK, let's try another approach.

    int x = 100, y = 20;

    Now run your program, enter some numbers, and tell me if it prints anything other than 2000.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by rolory
    I still don't understand what you're trying to say me.
    Really!?

    THe function call to rectangle() does not define a relationship is performs a calculation. At the time the calculation is performed, x and y have no defined value.

    You have to:
    1. get the values
    2. perform the calculation
    3. present the result

    not
    1. perform the calculation
    2. get the values
    3. present the result


    C does not work like a spreadsheet, you do not set formula and then have the formula calculate whenever the values change; you have to explicitly perform the calculation.
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by rolory
    Thank you.
    I still don't understand what you're trying to say me.
    Can you show me the correct code and to write me my errors in it?
    There are certain basic concepts that you seem to be having problems with. Concepts that are so basic and self-evident (to us, at least) that we cannot begin to understand having to explain them to anyone.

    1. Variables have values. When? Always! Who puts those values there? Simple answer is: most of the time you do, or at least you are supposed, but regardless they come into existence with a value already in them. What is that value? In C/C++, if it's a global variable, then that value is automatically zero. But if it's a local variable (inside a function), then that value could be anything at all, IOW random garbage, whatever just happened to have been written last to that memory location.

    I can give you a more complete explanation of what garbage is if you want. The bottom line is that you never want to use a garbage value. You always want a variable to contain a valid value when you use it.

    Whenever a programmer writes a program or reads a program, he is always thinking about what values the variables hold. Always. We know better than to use an uninitialized variable. We know that we need to ensure that a valid value has been assigned to a variable before we use it. It wouldn't even occur to us to even consider doing otherwise.

    You are trying to make use of two variables, x and y, before you had assigned any kind of value to them. That is what "uninitialized local variable used" means. The solution is to assign a value to those variables before you use them, a valid value, even a useful value such as what the user might input (hint!). That anybody could not understand that just boggles our minds.

    2. In sequential programming (which is what we're doing here), there is a sequence of events that the program follows. What determines that sequence? Primarily, the order in which the statements occur, from top to bottom -- secondarily the action of control structures, but even there the primary sequence is from top to bottom.

    Corollary to that is the simple self-evident fact that each statement causes an effect and that the effects of a statement are not created until that statement has been executed. That means that the order of the statements determines what happens and when. This is what clifford brought up and I think I recently noticed another beginner exhibiting the same problem.

    So then, back to your specific problem (not counting the inability to format code; my corrections included):
    Code:
    int main() 
    {
        int x,y;
        int rectangle_surface;
    
        // what are the values of x and y here?
        // x and y are not initialized.  They contain random
        //     garbage!
        // What possible purpose could there be to calling 
        //     rectangle() now?
        rectangle_surface=rectangle(x,y);
    
        // Oh, so now you decide to assign some 
        //     some values to x and y.  You're too late!
        //     That ship has already sailed!
        printf("Enter 2 ribs length of rectangle\n");
        scanf_s("%d %d", &x, &y);
    
        printf("%d\n", rectangle_surface);
        system("PAUSE");
        return 0;
    }
    Why can you not see that the sequence of your actions is wrong? Let's try an analogy. Paying your rent (or mortgage). First you mail payment envelope. Then you write the check to put in that envelope. Oh dear! You've already mailed that payment! You sent in the payment without the check! Do you think that that should work? Of course not! The order of the actions are completely wrong! So why do you think that the order of actions in your program is correct?

    Read the order of the sequence of actions in your program and keep in mind what the effects of those actions are. And always keep in mind what values your variables contain. Then you just might start to think like a programmer.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by dwise1_aol
    There are certain basic concepts that you seem to be having problems with. Concepts that are so basic and self-evident (to us, at least) that we cannot begin to understand having to explain them to anyone.

    1. Variables have values. When? Always! Who puts those values there? Simple answer is: most of the time you do, or at least you are supposed, but regardless they come into existence with a value already in them. What is that value? In C/C++, if it's a global variable, then that value is automatically zero. But if it's a local variable (inside a function), then that value could be anything at all, IOW random garbage, whatever just happened to have been written last to that memory location.

    I can give you a more complete explanation of what garbage is if you want. The bottom line is that you never want to use a garbage value. You always want a variable to contain a valid value when you use it.

    Whenever a programmer writes a program or reads a program, he is always thinking about what values the variables hold. Always. We know better than to use an uninitialized variable. We know that we need to ensure that a valid value has been assigned to a variable before we use it. It wouldn't even occur to us to even consider doing otherwise.

    You are trying to make use of two variables, x and y, before you had assigned any kind of value to them. That is what "uninitialized local variable used" means. The solution is to assign a value to those variables before you use them, a valid value, even a useful value such as what the user might input (hint!). That anybody could not understand that just boggles our minds.

    2. In sequential programming (which is what we're doing here), there is a sequence of events that the program follows. What determines that sequence? Primarily, the order in which the statements occur, from top to bottom -- secondarily the action of control structures, but even there the primary sequence is from top to bottom.

    Corollary to that is the simple self-evident fact that each statement causes an effect and that the effects of a statement are not created until that statement has been executed. That means that the order of the statements determines what happens and when. This is what clifford brought up and I think I recently noticed another beginner exhibiting the same problem.

    So then, back to your specific problem (not counting the inability to format code; my corrections included):
    Code:
    int main() 
    {
        int x,y;
        int rectangle_surface;
    
        // what are the values of x and y here?
        // x and y are not initialized.  They contain random
        //     garbage!
        // What possible purpose could there be to calling 
        //     rectangle() now?
        rectangle_surface=rectangle(x,y);
    
        // Oh, so now you decide to assign some 
        //     some values to x and y.  You're too late!
        //     That ship has already sailed!
        printf("Enter 2 ribs length of rectangle\n");
        scanf_s("%d %d", &x, &y);
    
        printf("%d\n", rectangle_surface);
        system("PAUSE");
        return 0;
    }
    Why can you not see that the sequence of your actions is wrong? Let's try an analogy. Paying your rent (or mortgage). First you mail payment envelope. Then you write the check to put in that envelope. Oh dear! You've already mailed that payment! You sent in the payment without the check! Do you think that that should work? Of course not! The order of the actions are completely wrong! So why do you think that the order of actions in your program is correct?

    Read the order of the sequence of actions in your program and keep in mind what the effects of those actions are. And always keep in mind what values your variables contain. Then you just might start to think like a programmer.
    Oh my god, I read all and I must say - thank you!!
    You taught my things I've been ever heard and taught myself. Thank you very much.
    I didn't know that if I don't declare the value of the variable, it value is garbage. I thought that if I don't initialize the variable and the program will request from the user insert value, the variable will include the value.
    Thanks so much, I really appreciate your comment, thanks a lot!!!

    Originally Posted by salem
    OK, let's try another approach.

    int x = 100, y = 20;

    Now run your program, enter some numbers, and tell me if it prints anything other than 2000.
    Yeah, it prints 2,000
    Originally Posted by clifford
    Really!?

    THe function call to rectangle() does not define a relationship is performs a calculation. At the time the calculation is performed, x and y have no defined value.

    You have to:
    1. get the values
    2. perform the calculation
    3. present the result

    not
    1. perform the calculation
    2. get the values
    3. present the result


    C does not work like a spreadsheet, you do not set formula and then have the formula calculate whenever the values change; you have to explicitly perform the calculation.
    Yes I know.. C works by the lines I write.. If I'll declare for example int x and int y after I write %d and %d, the compiler won't able to run the program.
  20. #11
  21. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by rolory
    If I'll declare for example int x and int y after I write %d and %d, the compiler won't able to run the program.
    It will run, it just won't do anything useful or predictable.

    You say you know that, but you have not applied that knowledge to your code and seemed confused when it was explained to you is several different ways.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by clifford
    It will run, it just won't do anything useful or predictable.

    You say you know that, but you have not applied that knowledge to your code and seemed confused when it was explained to you is several different ways.
    Excellent, thanks.
    By the way, I needed to define this call function line:
    PHP Code:
    rectangle_surface=rectangle(x,y); 
    after the compiler know what's x and y, I mean after this line:
    PHP Code:
    scanf_s("%d %d", &x, &y); 
  24. #13
  25. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by rolory
    By the way, I needed to define this call function line:
    PHP Code:
    rectangle_surface=rectangle(x,y); 
    after the compiler know what's x and y, I mean after this line:
    PHP Code:
    scanf_s("%d %d", &x, &y); 
    We know. That is exactly what every answer posted so far says in about as many ways as it is possible to say, you say it like you discovered that for yourself! :rolleyes:

IMN logo majestic logo threadwatch logo seochat tools logo