Thread: declare that a variable will be used later?

  1. #1
    cnoob()
    Join Date
    Mar 2015
    Posts
    28

    Question declare that a variable will be used later?

    I remember that the syntax exists I just don't remember what it is.

    I want to write a conditional function () prior to main (). in that function I'm using a variable that isn't introduced until main (). How do I let C know that I will be int'ng this function prior to the point where it is going to be accessed so that it doesn't freak out?

    alternatively is it possible to int the variable in both main() and my conditional function so that it exists? I imagine this would cause an issue because I would essentially be int'ng it twice.

    I've done the cursory google searches however I don't think I'm using the correct keywords to get the results I'm looking for.

    If necessary I would be more than happy to post the code it's just a simple 40 line homework assignment. thank you!!
    Last edited by SandyMan; 04-06-2015 at 08:23 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Program execution begins at "main()", so you can't have something that runs before "main()".

    You can, however, call your conditional function at the beginning of your program (first function call in "main()") and choose whether or not to execute the rest of the program (if that's its purpose).

    If the variable in your conditional function is needed in "main()", create the variable in "main()" and pass it to your function. Otherwise, it can just be local to your conditional function.

  3. #3
    cnoob()
    Join Date
    Mar 2015
    Posts
    28
    I have a declaration called
    acceptedvalues()
    at the beginning of the code, in there there is a for loop referring to a value that isn't introduced until main(). so if I int it in acceptedvalues() then main() is upset because acceptedvalies() is conditional, if I do it in main() then c is upset because it doesn't know what's going on when it sees it on line 6
    Last edited by SandyMan; 04-06-2015 at 09:27 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SandyMan
    at the beginning of the code, in there there is a for loop referring to a value that isn't introduced until main(). so if I int it in acceptedvalues() then main() is upset if I do it in main()
    That is why Matticus wrote:
    Quote Originally Posted by Matticus
    If the variable in your conditional function is needed in "main()", create the variable in "main()" and pass it to your function.
    Anyway, main won't be "upset": you can have local variables of the same name in different functions.
    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

  5. #5
    cnoob()
    Join Date
    Mar 2015
    Posts
    28
    ok, I must be doing something else wrong then. I'll keep working thanks!

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    120
    Quote Originally Posted by Matticus View Post
    If the variable in your conditional function is needed in "main()", create the variable in "main()" and pass it to your function.
    If it is something not context sensitive (such as some global state or constant), you can also declare it as global. Globaly declared names are not as bad as most people make them out to be, they do have to be used carefully, but they're not the devil's offspring.

    edit: I'm really curious as to what you're trying to do. Can you perhaps post the relevant parts of the code for us to check out?

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by shiroaisu View Post
    Globaly declared names are not as bad as most people make them out to be, they do have to be used carefully, but they're not the devil's offspring.
    Global variables might have their place, along with other concepts that are generally considered "bad practice" in many situations (e.g. goto).

    The reason these ideas tend to be discouraged as advice to people new to programming is because they are easy to abuse at that stage. New programmers might use these ideas as a crutch in the place of proper code construction (using globals instead of passing/returning values, or using goto instead of proper loop constructs, since it's "easier").

    Experienced programmers have the knowledge to use these things in acceptable situations (and also be aware of the potential pitfalls of their use).

    But by offering advice that requires knowledge and experience to implement in an acceptable fashion, while possibly fostering poorly written code, we would be bereft in our responsibilities for teaching.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declare functions with a variable's size
    By cable in forum C Programming
    Replies: 2
    Last Post: 02-01-2012, 03:43 PM
  2. Replies: 6
    Last Post: 02-07-2010, 01:22 PM
  3. getchar () and declare variable
    By sfff in forum C Programming
    Replies: 7
    Last Post: 10-19-2009, 12:23 AM
  4. Declare a variable in a switch
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 06-03-2008, 11:55 AM
  5. Declare an operator a variable?
    By bluenoser in forum C Programming
    Replies: 8
    Last Post: 10-19-2002, 09:45 AM