Passing by reference and value. Please explain prototypes and headers.

Hello, I'm studying functions right now and need clarification. I did a 'search' but the examples and explanations I read were still a bit cryptic to me.

How would I pass a function call that had a string in it? For example, here is a function call that holds an int and a string.

1
2
/*call from main*/
void learnPass(number, "John Doe");


Questions:
1. There must be int number definition in main, correct?
2. To pass by value, what would the prototype and function definition header look like?
3. To pass by reference, what would the prototype and function definition header look like?
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
#include <iostream>
#include <string>

void learnPassValue(int num, string str);
void learnPassRef(int& num, string& str);  //reference operator & in prototype

int main()
{
int number = 5;
string mystr = "John Doe";

learnPassValue(number, mystr);
cout << "in main after pass by value: " << number << endl;
cout << mystr << endl;

learnPassRef(number, mystr);
cout << "in main after pass by reference: " << number << endl;
cout << mystr << endl;

getch();
return 0;
}

void learnPassValue(int num, string str)
{
num = 2;
str = "value?";
cout << "in learnPassValue: " << number << endl;
cout << mystr << endl;
}

void learnPassRef(int& num, string& str)  //reference operator & in matching definition
{
num = 2;
str = "reference?";
cout << "in learnPassRef: " << number << endl;
cout << mystr << endl;
}
Last edited on
@Trac511
For example, here is a function call that holds an int and a string.
1
2
/*call from main*/
void learnPass(number, "John Doe"); 


void learnPass(number, "John Doe");

This is not a function call. It is an invalid syntactic construction. I think the internet (including this forum) has very many examples of function declarations and function calls. Try to find answers on such simple questions yourself.
Last edited on
would this work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int n = 1;
int main(){
	stringstream A;
	string B;
	A << n;
	A >> B;
	string C = C + "John Doe";
	cout << C << endl;
	system ("PAUSE");
}
Why do you need void, can't you just do it the way I did it?
Topic archived. No new replies allowed.