Creating Functions & passing information to other functions

This is a discussion on Creating Functions & passing information to other functions within the C Programming forums, part of the General Programming Boards category; I'm writting a piece of code that will use global and local values. Although when i try to compile it ...

  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
    22,165
    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);
    }
    
    /* ... */
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:31 PM
  2. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 04:13 PM
  3. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 07: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-21-2002, 12:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21