Help with functions

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void getname(string Names[5]);
void getscore (int Score[5]);
int average( int average,int total);
string getname(),Names;
int getscore(),Score;
int main()
{
	getname();
	getscore();
	cout<<Names<<endl;
}
void getname(string Names)
{
	cout<<"Plaese enter your names\n";
	for(int i=0; i<5;i++)
	{
		cin>>Names[i];
	}
	for(int i=0; i<5;i++)
	{
	return Names[i];
	}
}
void getscore(int Score)
{
	cout<<"Please enter your score\n";
	for(int i=0;i<5;i++)
	{
		cin>>Score;
	}
}


A lot of mistakes.

- 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

string getname(string Names[5]);
int getscore (int Score[5]);
int average( int average,int total);
int main()

{
	getname();
	getscore();
	cout<<Names<<endl;
}
string getname(string Names)
{
	cout<<"Plaese enter your names\n";
	for(int i=0; i<5;i++)
	{
		cin>>Names[i];
	}
	for(int i=0; i<5;i++)
	{
	return Names;
	}
}
int getscore(int Score)
{
	cout<<"Please enter your score\n";
	for(int i=0;i<5;i++)
	{
		cin>>Score;
	}
	return Score;
}
Last edited on
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
Alright so I guess I understand the void type know but as far as calling in the right parameters thought it would simply be the names of the given
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

string getname(string Names[5]);
int getscore (int Score[5]);
int average( int average,int total);
string Names;
int Score;


int main()
{
	getname( Names[i]);
	getscore(Score[i]);
	cout<<Names<<endl;
}
string getname(string Names[5])
{
	cout<<"Plaese enter your names\n";
	for(int i=0; i<5;i++)
	{
		cin>>Names[i];
	}
	for(int i=0; i<5;i++)
	{
	return Names[i];
	}
}
int getscore(int Score[5])
{
	cout<<"Please enter your score\n";
	for(int i=0;i<5;i++)
	{
		cin>>Score[i];
	}
		for(int i=0; i<5;i++)
	{
	return Score[i];
	};
}
You should read more about functions:
http://www.cplusplus.com/doc/tutorial/functions/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

using namespace std;

void getname(string *);
int getscore ();
void average( int ,int );


int main()
{
	string Names[5];
	getname(Names);
	int Score = getscore();
	
	//print names
	for (auto name: Names)
		cout << name << " ";
		
	//print average
	average(Score, 5);
}

void getname(string *Names)
{
	for(int i=0; i < 5 ;i++)
	{
		cin >> Names[i];
	}
}

int getscore()
{
	int totalScore = 0, temp;
	cout << "Please enter your score\n";
	for(int i=0;i<5;i++)
	{
		cin>>temp;
		totalScore += temp;
	}
	
	return totalScore;
}

void average( int totalscore, int num) {
	cout << "\nAverage of the class is: " << totalscore / num << endl;
}
Last edited on
You cannot pass in an array, what you can do is pass a pointer to an array. I did not test the code, but it should do what you want.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
using namespace std;

void setNames(string*, int);
void setScores(int*, int);

int main()
{
    const int NUM_PEOPLE = 10;  //we will have 10 people
    string* names;  
    int* scores; 
    names = new string[NUM_PEOPLE]; //empty array of string for names
    scores = new int[NUM_PEOPLE]; //empty array of ints for scores

    setNames(names, NUM_PEOPLE);
    setScores(scores, NUM_PEOPLE)

    cout << names << endl;
    cout << scores  << endl;
}

void setNames(string *names, int ammount)
{
    for(int i = 0; i < ammount; i++)
    {
        cin >> names[i];
    }
}

void setScores(int* scores, int ammount)
{
    for(int i = 0; i < ammount; i++)
    {
        cin >> scores[i];
    }
}

Because a pointer is a reference, returning it to itself is useless. That is why I made the functions void.

http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
Last edited on
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.
Read the links I provided, they will explain functions really well.
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
Here is an example of a function that would add something.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//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
Last edited on
Topic archived. No new replies allowed.