Are these valid functions?

void processData(string nameP, string & addrP, float & salaryP, int employP)

blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah

processData("Ms. Shirley Sithole", address, sallary, 88123); //VALID OR NOT?

processData(name, "45Ashley Road, Birch Court, Kempton Park", salary, employeeNumber); //VALID OR NOT?


processData("Who are you?", address, 8750.00, 909); //VALID OR NOT?




It depend's on what you actually wrote as the function body. Include more info if you want a answer.

Right now most of them are accurate if you are trying to call a function, but rather it's compilable depends on you function parameter and body.
Last edited on
1. #include <iostream>
2. #include <string>
3. using namespace std;
4. void processData(string nameP, string & addrP, float & salaryP,
int employP)
5. {
6. if (nameP == "Who are you?")
7. nameP = "Mr. Ghost";
8. if (salaryP > 10000.00)
9. salaryP += 0.08 * salaryP;
10. else
11. salaryP += 0.10 + salaryP;
12. cout << "New salary for employee number " << employP << " : R"
<< salaryP << endl;
13. }
14. int main()
15. {
16. string name, address;
17. float salary;
18. int employeeNumber;
19. cin >> name >> address >> salary >> employeeNumber;
20. processData("Ms. Shirley Sithole", address, salary, 88123);
21. employeeNumber = 54321;
22. name = "Mr. John Smith";
23. processData(name, "45 Ashley Road, Birch Court, Kempton Park",
salary, employeeNumber);
24. processData("Who are you?", address, 8750.00, 909);
25. return 0;
Given this declaration:
void processData(string nameP, string & addrP, float & salaryP, int employP)


This is valid:
processData("Ms. Shirley Sithole", address, sallary, 88123);

These are invalid (I've underlined the offending arguments):
1
2
processData(name, "45Ashley Road, Birch Court, Kempton Park", salary, employeeNumber);
processData("Who are you?", address, 8750.00, 909);


Why? Because when you pass an object by reference, you actually need the object to exist with an address.

In the first call, use
1
2
string address = "45Ashley Road, Birch Court, Kempton Park";
processData(name, address , salary, employeeNumber);


For the second call use:
1
2
float salary = 8750.f
processData("Who are you?", address, salary, 909);

Last edited on
Thanks for the reply


how come processData("Ms. Shirley Sithole", address, sallary, 88123); a valid function?

int number = 100;
string game = “poker”;
int members;
int rounds = 6;
playGame(number, “blackjack”, members, 3);

are these valid:

1. void playGame(int numberP, string & gameP, int membersP,
int roundsP);
2. void playGame(int & numberP, string gameP, int & membersP,
int roundsP);

anyone?
Tunafish wrote:
how come processData("Ms. Shirley Sithole", address, sallary, 88123); a valid function?


It's because arguments 1 and 4 require just data, the function makes its own copy of that data.

Arguments 2 and 3 require references which really means that the address of the data is passed through, not the data itself. This means that if you change the variable, you'll change the original value in the calling function as well.

As for your second question:
In the declaration/definition of the function, just put the & symbol where one of the two conditions are met:
1. You need to change the original value (two way communication through a function call).
2. You have a large data-type (a huge class) that will take a while to copy.
Thanks
Topic archived. No new replies allowed.