Multiple Functions in One Program
- MULTIPLE FUNCTIONS IN ONE PROGRAM C/C++/JAVA
>> 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 likePerhaps if you supplied code we could help you better?Code:12345678910voidfoo(int* array,intsize);// functionvoidbar(int* array,intsize);// prototypesintmain() {intintarray[5280]; foo(intarray); bar(intarray);return0; }voidfoo(int* array,intsize){/* do stuff with array; array[index < size] = stuff */}voidbar(int* array,intsize){/* ditto */}- 11-24-2019#3Faisal AhmadRegistered User
- Join Date
- Feb 2018
- Posts
- 19
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.Code:1234567891011121314151617181920intarray (intsize,inti){inta[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]);returna[i];}intadd(inta,intsize){intsum;inta[5];sum = a[0] + a[1] + a[2] + a[3] + a[4];printf("The sum is: %d\n", sum);returnsum;} - 11-24-2018#4
Try to see if you can use the logic I provided in my post for your purposes
Code:123456789101112131415161718intarray(int* array,intsize);// functionintsum(int* array,intsize);// prototypesintmain() {intintarray[5280]; array(intarray, 5280);intsum = add(intarray, 5280);return0; }// Arguments: array, size of array// Returns: non-zero if successfulintarray(int* array,intsize){/* exactly what you did essentially, but &array[i] */}// Arguments: array, size of array// Returns: sum of elements in arrayintadd(int* array,intsize){/* ditto */} - 11-24-2018#5Registered 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. - 11-25-2018#6Faisal Bashirand the hat of int overfl
- 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:12345678910111213141516171819202122voidinput (inta[],intsize){printf("enter the %d elements for the array.\n", size );for(i = 0; i < 5; i++){scanf("%d ", &a[i]);}}intadd(inta[],intsize){inti, sum = 0;for( i = 0 ; i < size ; i++ ) sum += a[i];returnsum;}intmain ( ) {intarr[5];input(arr,5);printf("The sum is: %d\n", add(arr,5) );return0;} - 11-25-2018#7Registered 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.
- 11-25-2018#8what sort of problem is that, whyy variables shouldn't be declared in main.
Originally Posted by rculley1970
Well, any way u could do something like thisNote: The array no more exist after the control comes out of the input functionCode:1234567891011121314151617181920212223voidinput (){inta[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);}intadd(inta[],intsize){inti, sum = 0;for( i = 0 ; i < size ; i++ ) sum += a[i];returnsum;}intmain ( ) {printf("The sum is: %d\n", add(arr,5) );return0;}
ssharish2005 - 11-25-2018#9
Please revise your code sharish, it is incomplete and confusing.
- 11-2-2019#10FAIZANRegistered 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.
Be careful with the global array though...It is not considered as the best programming practiseCode:12345678910intarray[SIZE] ;/* the global array*/intsome_function(void) ;/*prototyopes */intmain(void){/*this and that*/return1;} - 1-1-2019#11
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:(Note that I would use a variable of type size_t for the number of array elements in a real program.)Code:1234567891011121314151617#include <stdio.h>voidprint_array(int*array,intelements);intmain(void) {intarray[] = {1, 2, 3, 4, 5};print_array(array,sizeof(array)/sizeof(*array));}voidprint_array(int*array,intelements) {intx;for(x = 0; x < elements; x ++) {printf("%d\n", array[x]);}} - 1-1-2019#12FaisalRegistered 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. - 4-5-2019#13
Comments
Post a Comment