Hi I am just learning about functions and I have the general idea of how an array works but How do I do function such as define them and call on them. In this program I am trying to get 5 names and scores then average them up
- A void function does not return anything. (line 27)
- A function can only return values that are the same type as the function - exception is void functions which return nothing (line 27)
- On line 7, you have declared a prototype for a function of type void which takes an array of strings as parameter. Then on line 18 you define a function which takes a string as parameter. And on line 10, you have defined another function prototype that takes nothing as a parameter. Then finally on line 14 you call one of the overloaded getname functions except that the one you called is not defined :s.
- This is the same for getscore
- You can't just declare functions however you please and expect that by some compiler magic, calling one of the functions would automatically result in a function that does what you need
Alright following what you said I changed the void types to string and int. but then how do you call a function in a program if you can't just do as I did. , but at the same time what is the point of Viod if it doesn't return anything
As you get more comfortable with programming, you will come to realise that not all the time you call a function are you expecting it to return anything to you. Some functions may be called to perform a specific task like write information to a file, or print a welcome message, or (when you get comfortable with pointers/references) manipulate an object.
As for your program, it is getting there however still a few mistakes:
- line 17 should be the same as line 7 (without the semicolon)
- line 29 should be the same as line 8 (again no semicolon)
- line 13 should have the correct parameters* when calling getname
- line 14 should have the correct parameters* when calling getscore
- line 15 you have not defined a variable callled 'Names' so this line should be fixed
*Note by parameters I mean the right variable must be placed between the round brackets when the functions are being called
I don't know that explains how to call a function that does things like add or subtract and even how to use a void function to display information and such but with something like getting an array of input how would a function like that be called with not data input.
//This function takes in two int arguments x and y.
//Adds them into a variable called answer, and returns that variable.
int add(int x, int y)
{
int answer = x + y;
return answer;
}
//This function takes in one argument, number
//And prints it after "Your answer is: "
void display(int number)
{
cout <<"Your answer is: " << number << endl;
}
int main()
{
int num1, num2, answer;
cout << "Enter the first number:";
cin >> num1;
cout << "Enter the second number:";
cin >> num2;
answer = add(num1, num2); //calls the add function with num1 and num2
//as parameters
display(answer); //calls the display function with answer as parameter
}
This program in the command line would look like this.
1 2 3
Enter the first number: 5
Enter the second number: 6
Your answer is: 11