Multiple functions

This is a discussion on Multiple functions within the C Programming forums, part of the General Programming Boards category; I am trying to create a program with multiple functions before the main function. I am trying to use an ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    19

    Multiple functions

    I am trying to create a program with multiple functions before the main function. I am trying to use an array and use the element data in all the different functions but I am getting an error saying that the int wasn't declared. How do i use the same array data throughout all the funtcions?

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Pass the value around using a pointer. Or use a global. Or something wierd that the OS supplies you with.

    >> I am trying to create a program with multiple functions before the main function

    You can declare the functions before main (or in a header) and then define the function wherever you like

    Code:
    void foo(int * array, int size); // function
    void bar(int * array, int size); // prototypes
    
    int main() { int intarray[5280]; foo(intarray); bar(intarray); return 0; }
    
    void foo(int * array, int size) 
    { /* do stuff with array; array[index < size] = stuff */ }
    
    void bar(int * array, int size) 
    { /* ditto */ }
    Perhaps if you supplied code we could help you better?

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    19
    Code:
    int array (int size, int i)
     {
    	int a[5];
    	printf(" enter the 5 elements for the array.\n");
    	for (i = 0; i <= 4; i++)
    	{
    		scanf("%d ", &a[i]);
    	}
    printf("%d, %d, %d, %d, %d\n", a[0], a[1], a[2], a[3], a[4]);
    	return a[i];
    }
    
    int add(int a, int size)
    {
    	int sum;
    	int a[5];
    	sum = a[0] + a[1] + a[2] + a[3] + a[4];
    	printf("The sum is: %d\n", sum);
    	return sum;
    }
    This is just 2 functions. trying to get the data entered into the first one to be used for the second. I know it is very flawed and it is for a class so only requesting hints, not code.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Try to see if you can use the logic I provided in my post for your purposes

    Code:
    int array(int * array, int size); // function
    int sum(int * array, int size); // prototypes
    
    int main() { int intarray[5280]; array(intarray, 5280); int sum = add(intarray, 5280); return 0; }
    
    
    // Arguments: array, size of array
    // Returns: non-zero if successful
    
    int array(int * array, int size) 
    { /* exactly what you did essentially, but &array[i] */ }
    
    
    // Arguments: array, size of array
    // Returns: sum of elements in array
    
    int add(int * array, int size) 
    { /* ditto */ }

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    19
    I am putting the funtions before main(), only the function calls are being put into main(). I get the error:

    error C2109: subscript requires array or pointer type

    for my array in the second function when I am trying to use the data entered into the first function. I have cleaned up the first function so it works right but I am trying to get the second function to use the data and the same array in the first one. Will work on it a little more tonight then hit it again in the morning.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    You can't pass and return arrays like you can with integers.

    So you have to decide who is in overall charge (say main), and let it declare the array. Then you pass that array (as a pointer) to all the interested functions to do their bit.
    Code:
    void input (int a[], int size)
    {
    	printf("enter the %d elements for the array.\n", size );
    	for (i = 0; i < 5; i++)
    	{
    		scanf("%d ", &a[i]);
    	}
    }
    
    int add(int a[], int size)
    {
    	int i, sum = 0;
    	for ( i = 0 ; i < size ; i++ ) sum += a[i];
    	return sum;
    }
    
    int main ( ) {
    	int arr[5];
    	input(arr,5);
    	printf("The sum is: %d\n", add(arr,5) );
    	return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    19
    Unfortunately I cannot have any variables or arrays declared in the main() function. The only thing I can use in main are the function calls. I also don't understand how arr[5] can be declared for a[]. Granted that code works, although I cannot use it.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by rculley1970
    Unfortunately I cannot have any variables or arrays declared in the main() function. The only thing I can use in main are the function calls. I also don't understand how arr[5] can be declared for a[]. Granted that code works, although I cannot use it.
    what sort of problem is that, whyy variables shouldn't be declared in main.

    Well, any way u could do something like this

    Code:
    void input ()
    {
    	int a[5]
                    printf("enter the %d elements for the array.\n", size );
    	for (i = 0; i < 5; i++)
    	{
    		scanf("%d ", &a[i]);
    	}
    
                     printf("The sum of all numbers is %d", add(a,5);
    }
    
    int add(int a[], int size)
    {
    	int i, sum = 0;
    	for ( i = 0 ; i < size ; i++ ) sum += a[i];
    	return sum;
    }
    
    int main ( ) {
    	printf("The sum is: %d\n", add(arr,5) );
    	return 0;
    }
    Note: The array no more exist after the control comes out of the input function

    ssharish2005

  9. #9
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Please revise your code sharish, it is incomplete and confusing.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  10. #10
    Registered User
    Join Date
    Nov 2006
    Location
    Greece
    Posts
    10
    Do you have some interface you must implement?If not ,as Tonto already said , a global array would do the work.
    Code:
     
    int array[SIZE] ;/* the global array*/
    
    int some_function( void ) ; /*prototyopes */
    
    int main( void )
    {
       /*this and that*/
    
      return 1;
    }
    Be careful with the global array though...It is not considered as the best programming practise

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Usually one returns 0 from main() to indicate success.

    There's no reason to use a global variable -- passing an array between functions isn't too hard. Have a look at another example:
    Code:
    #include <stdio.h>
    
    void print_array(int *array, int elements);
    
    int main(void) {
        int array[] = {1, 2, 3, 4, 5};
    
        print_array(array, sizeof(array)/sizeof(*array));
    }
    
    void print_array(int *array, int elements) {
        int x;
    
        for(x = 0; x < elements; x ++) {
            printf("%d\n", array[x]);
        }
    }
    (Note that I would use a variable of type size_t for the number of array elements in a real program.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Nov 2006
    Location
    Greece
    Posts
    10
    Thanx for the tips dwks,
    As far as the globlal array is concerned , I think it fits best in the specific situation. I mean it seems a bit strange that he is supposed not to declare any variables in main , just calling the functions so i assumed that they should use a global array.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Right, I missed that criteria. A global array is the best idea, then.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Feb 2006
    Posts
    19
    I appreciate everyones help, I just got on and am looking at the help and working on it now. I don't want to go too far into explaining since it is for a class so I just want hints provided as I don't want to get busted for plagerism. I will be outlining that I recieved help from this board in my discussion on the project. I will look into the global array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  2. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  3. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  4. Recursion vs multiple functions
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2002, 07:52 PM
  5. Passing data/pointers between functions #2
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 08:49 PM

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