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 likeCode:12345678910void
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 */
}
- 11-24-2019#3Faisal AhmadRegistered User
- Join Date
- Feb 2018
- Posts
- 19
Code:1234567891011121314151617181920int
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;
}
- 11-24-2018#4
Try to see if you can use the logic I provided in my post for your purposes
Code:123456789101112131415161718int
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 */
}
- 11-24-2018#5
- 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:12345678910111213141516171819202122void
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;
}
- 11-25-2018#7
- 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#8
Originally Posted by rculley1970
Well, any way u could do something like thisCode:1234567891011121314151617181920212223void
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;
}
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.
Code:12345678910int
array[SIZE] ;
/* the global array*/
int
some_function(
void
) ;
/*prototyopes */
int
main(
void
)
{
/*this and that*/
return
1;
}
- 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:Code:1234567891011121314151617#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]);
}
}
- 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