Thread: Functions

  1. #1

    Question Functions

    just a simple question:

    what is the problem with this code:

    #include <stdio.h>



    void main()
    {

    int test(int number); /* function prototype */
    int i;
    int result = 0;


    result = test(number);

    for(i = 0; i < 20; i++)
    printf("\n %d \n %d \n\a", i, result);
    }

    int test(int number)
    {
    number = 5;

    return number;
    }


    and does a pointer have to have an astric (*) before it?

  2. #2
    Registered User jasrajva's Avatar
    Join Date
    Oct 2001
    Posts
    99
    result = test(number);
    now how can you do that

    number by itself is a variable name local to your test fn so to do the above you must declare an int number in main or as a global

    declare in after orbfrore yr test prototype as

    int number = 0;

    also void main should be
    int main()
    read the faqs to know why
    jv

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yeah, you see, the scope of your var 'number' is not what you think it is:
    Code:
    int test(int number) 
    { 
    number = 5; 
    
    return number; 
    }
    'number' is only "seen" by this function as the parameter to the function.
    Code:
    result = test(number);
    You never declared number as an int in 'main'. The correct code would be:
    Code:
    #include <stdio.h> 
    
    
    
    void main() 
    { 
    
    int test(int number); /* function prototype */ 
    int i; 
    int result = 0; 
    int number;
    
    
    result = test(number); 
    
    for(i = 0; i < 20; i++) 
    printf("\n %d \n %d \n\a", i, result); 
    } 
    
    int test(int number) 
    { 
    number = 5; 
    
    return number; 
    }
    And to your second question, a pointer has to have an * for declaration and to get the value of the pointer.

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM