Thread: Function prototype questions

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Function prototype questions

    Here's some hypothetical pseudocode. I'm trying to understand the relationship between function prototypes and main:

    Code:
    function main{
    int x
    int y
     } // end function main
    
    function prototype A
    int a
    
    function prototype B
    int b
    1. Can function prototype A call int x? Or does int x need to be defined in A?

    2. Can function B use int a?

    3. Is it possible to call prototype A, return int a to main, and call int a inside prototype B?
    THE redheaded stepchild.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your use of the terminology is confusing. By prototype I'll assume you mean definition, and I'll assume that the variables a and b are declared in functions A and B respectively.

    1. No, A cannot use x.
    2. No, B cannot use a.
    3. Again, wrong terminology. You don't call a variable.

    You can use a globally defined variable anywhere. It is better practice to define your variables within the function or block that they are used. You can pass these variables as arguments to functions that need to use their values. You can pass-by-reference if you need to have the local value updated when you pass a variable to a function. You can also use a return value to return a value from a function to the code that called that function.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Prototypes don't contain anything they are just the function headers howerver functions can't access other functions local variables.

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    1. No it can't, its not in a scope A can see (inside of A, or global), and otherwise would need to be passed as a parameter.

    2. Yes.

    3. Uh, no. Actually main() isn't can't call either of those prototypes because they are below main(). If you fixed that though.. Its not calling prototype A, its calling function A. If function A is defined under the global variable, then it can use that variable. Then if main() is below the prototype for that function then main() can call that function.

    You could easily have tested these out for yourself.
    Would have been better if it was real code to explain, not pseudocode in this case.

    Code:
    2. No, B cannot use a.
    Function, and prototype?, B are both located under the global variable a and can use that variable. Since hes getting the terminology wrong I'm going to assume thats the function. So B could use a.
    Last edited by Dae; 11-29-2005 at 12:11 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 2. Yes.

    If a and b are global variables, not declared inside A and B respectively, then you are correct.

    Of course, if that's what the OP meant, then it is a really simple answer. You can only use things that are declared or prototyped above the code that tries to use it.

  6. #6
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Daved
    Of course, if that's what the OP meant, then it is a really simple answer. You can only use things that are declared or prototyped above the code that tries to use it.
    Yeah, and I assumed they weren't declared in the functions because 1) he calls them prototypes, and 2) he used curly braces for main() but not those two.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Thank you for the clear explanation Daved, as you can tell I'm still new to this and attempting to use the correct terminology. Your comments make sense.
    THE redheaded stepchild.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM