Simplest question ever! Sorry i can't work it out...!- Local variables

This is a discussion on Simplest question ever! Sorry i can't work it out...!- Local variables within the C Programming forums, part of the General Programming Boards category; I am trying to ditch my global variables but i can't work out how to send a variable between functions... ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    18

    Question Simplest question ever! Sorry i can't work it out...!- Local variables

    I am trying to ditch my global variables but i can't work out how to send a variable between functions...

    eg.

    Code:
    int main(float x)
        {
            calculate();
    
            printf("x = %f",x);
            return 0;
        }
    
    int calculate()
        {
            float x;
            x=2+2;
        }
    The program runs but gives an answer of x = 0.

    What am i doing wrong?

    Thanks,

    Jez

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    You should not have main() accepting a float parameter.

    You need to take an integer (or two) as parameter(s) for calculate(), then, pass a local variable's address in main() to calculate's parameter(s) and do the summing, etc.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    yes that is what i'm trying to do!

    please can you show me how?

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Here would be a sample program:

    Code:
    void addtwo(int *);
    
    int main(int argc, char **argv)
    {
        int x = 2;
    
        addtwo(&x);
    
        printf("%d", x);
    
        return 0;
    }
    
    void addtwo(int *toadd)
    {
        *toadd += 2;
    }

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    is there no way to just send the value of a variable from one function to another?

    thanks,

    Jez

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by JezW View Post
    is there no way to just send the value of a variable from one function to another?

    thanks,

    Jez
    Of course. Instead of passing the variable's address as a parameter, pass the variable itself.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    since u didnt yet do pointers u can by return values here simple code to demonstrate it
    Code:
    #include <stdio.h>
    int calculate(int num,int nums)
    {
            float x;
            return x=num +nums;
    }
    
    int main(void)
    {
        int result;
        result=calculate(2,2);//here u enter the numbers u wanna plus
        printf("%d",result);
        getchar();
        return 0;
    }

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    Quote Originally Posted by abraham2119 View Post
    Here would be a sample program:
    Have a read, please:
    SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    18
    Quote Originally Posted by abraham2119 View Post
    Of course. Instead of passing the variable's address as a parameter, pass the variable itself.
    and how might one go about this?

    Jez

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JezW View Post
    and how might one go about this?

    Jez
    That was described in post #7 in this thread.

    Note, however, that if you just pass the value, there is no way [1][2] that the code INSIDE the function can modify the value outside of the function - it has no access to the value.

    [1] If the parameter is an array, it becomes a pointer, and in this way, it IS possible to modify parameters that are arrays.

    [2] Beginners: Please ignore this part. Yes, a function can do anything it like, and it can, given some relatively restricted conditions modify data that was passed in by creating it's own pointer, and move back beyond the stack-frame of the current process. But we are talking the same sort of thing as if I said "you as a driver of a car can not affect other drivers of other cars" and then someone saying "But if my car is bigger/stronger, I can push the other cars around on the road" - of course, that is POSSIBLE, but you are not doing things how they should be done, and you can't do that all the time, since some cars will probably be bigger than yours. I'm just covering the fact that some pedant will point out the flaw in my categorical statement above [as I would do]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about some variables in my code
    By XodoX in forum C Programming
    Replies: 4
    Last Post: 02-03-2009, 05:29 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. how to access local variables outside loops?
    By cdkiller in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2006, 06:48 PM
  4. local and global variables???
    By geo_c in forum C Programming
    Replies: 5
    Last Post: 08-23-2004, 03:02 PM
  5. local variables
    By Unregistered in forum C Programming
    Replies: 14
    Last Post: 03-20-2002, 01:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21