Thread: rtn value from function question

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    37

    rtn value from function question

    I have two files....main.c and func.c
    I want to initialize a value globally in main.c
    I want to send func.c parameters and for a function within that file to be able to see (not pass) the global variable I initialized. I then want that func within func.c to act on the parameters then change the global variable and return its value....Within main I will then act upon this value and clear it....The next time function within func.c gets called I would like it to recognize that main modified the variable....and so forth....

    Basically I want to init and clear a variable in main and when func gets called it also modifies that variable....During the init and clear within main I will use the modified variable from func. I do NOT want to clear this variable within func ...

    Any ideas?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,910
    Quote Originally Posted by ridgerunnersjw
    I want to send func.c parameters
    Did you mean that you want to pass actual parameters (i.e., arguments) to functions defined in func.c from functions in main.c? That should not be a problem: just call those functions from the functions in main.c

    Quote Originally Posted by ridgerunnersjw
    for a function within that file to be able to see (not pass) the global variable I initialized
    That certainly can be done: just forward declare the global variable by declaring it as extern before that function (or even at the start of that function's body).

    Quote Originally Posted by ridgerunnersjw
    then change the global variable and return its value
    Since you're modifying the global variable, returning its value is pointless: the caller -- the main function as you have indicated -- can just access the global variable for its updated value. This is both the boon and the bane of global variables: they can easily be modified and accessed from anywhere.

    Quote Originally Posted by ridgerunnersjw
    Within main I will then act upon this value and clear it
    That just means accessing and modifying the global variable. What exactly do you mean by "clear it" though? If that means resetting its value to a fixed known value, then it looks like you don't need a global variable: rather just initialise a local variable in the function that is called by main, and then return the value to main for main to act upon it.

    Quote Originally Posted by ridgerunnersjw
    The next time function within func.c gets called I would like it to recognize that main modified the variable
    When you say "recognize that main modified the variable", do you mean "use the global variable with its updated value", which of course will be done just by accessing it, or do you really want to track that the main function made a modification to that global variable?

    Quote Originally Posted by ridgerunnersjw
    I do NOT want to clear this variable within func
    Why not? As I noted earlier, if main is always "clearing" this variable by resetting it to a constant before calling this function, then you might as well change it to a local variable that is initialised by the same constant in that function, and then return its value for main to use. An exception would be if this variable is expensive to copy, e.g., it is a large array or struct, then you could declare it in main instead and pass a pointer to it as an argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2020
    Posts
    37
    The problem is the function is getting called at the beginning of wakeup every time HOWEVER my main code relies on the functions previous call return values to make some decisions THEN clear out all the variables....Its somewhat complicated however with that said I believe that extern is the way to go.....Thank you....I will look into this but at the surface I think that will work....I will define it as global in the main file then the func.c will have access to it....If I do this and within func.c it changes the state of the variable will I be able to see it's changed state back in main then? I'm guessing I can.
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-19-2020, 05:50 AM
  2. Function Question
    By Aliano in forum C Programming
    Replies: 4
    Last Post: 10-30-2013, 01:38 PM
  3. a question on the use of pow() function
    By brassbin in forum C Programming
    Replies: 8
    Last Post: 10-18-2013, 04:51 AM
  4. Function Question
    By Massaker in forum C Programming
    Replies: 3
    Last Post: 09-10-2005, 01:59 PM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM

Tags for this Thread