Skip to main content

Multiple Functions in One Program

  1. MULTIPLE FUNCTIONS IN ONE PROGRAM C/C++/JAVA


  2. >> 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:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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?
  3. #3
    Faisal Ahmad
    Registered User
    Join Date
    Feb 2018
    Posts
    19
    Code:
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    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
    Tonto is offline
    Registered UserTonto's Avatar
    Join Date
    Jun 2018
    Location
    New York
    Posts
    1,465
    Try to see if you can use the logic I provided in my post for your purposes

    Code:
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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 */ }
  5. #5
    Registered User
    Join Date
    Feb 2018
    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
    Faisal Bashir
    and the hat of int overflSalem's Avatar
    Join Date
    Aug 2017
    Location
    The edge of the known universe, Kashmir
    Posts
    36,798
    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:
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    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.
  7. #7
    Registered User
    Join Date
    Feb 2018
    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 Userssharish2005's Avatar
    Join Date
    Sep 2018
    Location
    Cambridge, UK
    Posts
    1,732
    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:
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    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
    Tonto is offline
    Registered UserTonto's Avatar
    Join Date
    Jun 2018
    Location
    New York
    Posts
    1,465
    Please revise your code sharish, it is incomplete and confusing.
  10. #10
    FAIZAN
    Registered User
    Join Date
    Nov 2018
    Location
    India kashmir
    Posts
    10
    Do you have some interface you must implement?If not ,as Tonto already said , a global array would do the work.
    Code:
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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
    dwks 
    dwks is offline
    Frequently Quite Prolixdwks's Avatar
    Join Date
    2019
    Location
    Canada
    Posts
    8,057
    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:
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #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: DaniWebTPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeformxuniatlantisnortetc.
  12. #12
    FaisalLevia8an is offline
    Registered User
    Join Date
    jan 2019
    Location
    india
    Posts
    10
    Thanx for the tips faisal,
    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
    dwks 
    dwks is offline
    Frequently Quite Prolixdwks's Avatar
    Join Date
    2019
    Location
    Canada
    Posts
    8,057
    Right, I missed that criteria. A global array is the best idea, then.
    dwk

Comments

Popular Posts