Function Arrays problem

Started by
3 comments, last by rip-off 11 years, 8 months ago
I am having trouble sending a user defined function to another user defined function. The other tricky part to this is sending an array to another function. Here is my code..


#include <iostream>

using namespace std;

const int size = 10; // Number of elements

int store_input(int [], int); // User input
int calculation(int[], int); // Calculating average
void show(int []); // Displaying data

int main()
{
int store_score[size] = {0,0,0,0,0,0,0,0,0,0}; // Store values array

store_input(store_score, size);
calculation(); // Calculating average
show(); // Displaying data






system("Pause");
return 0;
}


int store_input(int arr[], int size)
{
int send_score = 0; // Send to another function
int i;
cout << "Enter your score: " << endl;
for(int i = 0; i < size; i++)
{
cin >> arr;
send_score = send_score + arr;
}

return send_score;
}

int calculation(int send_score, int size)
{

int hold = store_input(store_score, size);
int average = 0;

average = hold / 10;

return average;

}

void show()
{
int show = calculation(int send_score, int size);


cout << "The average is: " << show << endl;

}



**** Sorry for the sloppy code, I was trying to give you an idea of what I was trying to do ********

In this program, I am trying to send user input from the store_input() to the calculation(). The calculation function will then return the average which it will deliver to the show(). The show function will then display the average. I need an explanation on how I can send one function to another ? Also a quick explanation on how I should treat function arrays especially when transferring arrays to another function.
Advertisement
1) A function needs to be called with the same number of parameters as it is declared with -- in this case calculation needs to be called with 2 arguments.
2) Don't call store_input inside calculation, and calculation inside show-- this will not get the old value they returned before; it will repeat the contents of the function. Instead, save the result of the functions in main and pass them to later functions.
Each of your functions needs to have a matching declaration and definition.

Note the mismatch in the "calculation function":

int calculation(int[], int);

int calculation(int send_score, int size)
{
// ...
}


Also for the "show" function:

void show(int []);

void show()
{
// ...
}


Your main() function is not calling the above functions with the correct arguments either.

Note that because a definition doubles as a declaration, the simplest solution for a single file is to re-order the functions so that no extra declarations are needed:

int store_input(int arr[], int size)
{
// ...
}

int calculation(int send_score[], int size)
{
int hold = store_input(store_score, size);
// ...
}

void show(int send_score[], int size)
{
int show = calculation(int send_score, int size);
// ...
}


int main()
{
// ...
}


Note in the code below, to call a function you need to omit the "int" types here. This syntax is an illegal mixture between a function call expression and a function declaration.

int show = calculation(int send_score, int size);


The correct code would look like:

// These variables must already exist, with the following types:
// int send_score[]
// int size

// Call the function
int show = calculation(send_score, size);
@King Mir

Thank you for the help, I am just curious on whether they're are cases where it is possible to send one function to another without including main() in C++

I am just curious on whether they're are cases where it is possible to send one function to another without including main() in C++
[/quote]
Can you explain what you mean by "send one function to anther"?

From the comment in your original code, it sounds like you are talking about returning a value from a function to a caller. If so, your question is presumably whether you can return a value to a caller who isn't main?

Yes, you can. In fact, in larger programs most function calls and return values will not be to/from main, but instead nested inside yet other functions.

In your case it makes more sense for main() to call each of these functions, because that way your functions don't depend on one another. Functions with few or no dependencies are easier to test and understand than functions with more dependencies.

This topic is closed to new replies.

Advertisement