Thread: Creating Functions & passing information to other functions

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    42

    Creating Functions & passing information to other functions

    I'm writting a piece of code that will use global and local values. Although when i try to compile it will not let me pass values to different functions. what am i doing wrong

    Code:
    #include <stdio.h>
    
    int a, b;
    long int c, d, total;
    
    int main()
    {
    	total = inputnumbers();
    	printf("The sum of these numbers is %i", total);
    }
    
    int inputnumbers()
    {
    	printf("Please enter integer 1 : ");
    	scanf("%i", &a);
    
    	printf("Please enter integer 2 : ");
    	scanf("%i", &b);
    
    	printf("Please enter long integer 1 : ");
    	scanf("%i", &c);
    
    	printf("Please enter long integer 2 : ");
    	scanf("%i", &d);
    
    	return addints(a,b,c,d);
    }
    
    int addints(int w, int x, long int y, long int z)
    {
    	long int sum;
    	sum = (long)w + (long)x + y + z;
    
    	return sum;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is about the order of visibility, reading from the top of the page down. C can't call a function it hasn't already seen... Since inputnumbers calls addints, it has to see addints before you can call it. Since main calls inputnumbers, it has to see inputnumbers first.

    The correct order would be

    addints()
    inputnumbers()
    main()

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    42
    Your actually a wizard, thanks so much.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by RyanLeonard
    I'm writting a piece of code that will use global and local values.
    Avoid global variables.

    Quote Originally Posted by CommonTater
    The correct order would be

    addints()
    inputnumbers()
    main()
    Indeed. Alternatively, you can forward declare addints and inputnumbers, e.g.,
    Code:
    #include <stdio.h>
    
    int inputnumbers(void);
    int addints(int w, int x, long int y, long int z);
    
    /* ... */
    
    int main()
    {
    	total = inputnumbers();
    	printf("The sum of these numbers is %i", total);
    }
    
    /* ... */
    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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RyanLeonard View Post
    Your actually a wizard, thanks so much.
    No wizardry involved... it's just how the language works.

    Lazerlight has shown you the alternative. It's probably just a matter of style, but I prefer to keep the order of functions rather than use prototypes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  2. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  3. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  4. Replies: 4
    Last Post: 04-01-2003, 12:49 AM
  5. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM