functions

Pages: 12
Hey guys. Im trying to learn functions but i seem to have a bit of a problem. Heres a simple function example of me trying to use a statement from one function to another.
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
#include <iostream>
#include <string>

using namespace std;

string getusername();
void name();

int main()
{

	string mainsName;

	name();

	mainsName = getusername();

	cout<<"You entered " << mainsName<< endl;
	cin.ignore();
	cin.ignore();
	system ("cls");

	cout<<"Hi "<<mainsName<<" Is this working?"<<endl;

	cin.ignore();
}

void name()
{
	cout<<"Please enter your name: ";
}

string getusername()
{
	//Local variables

	string aname;

	////////


	cin >> aname;

	return aname;
}


and this work great for main. however how would I use it if i wanted to input this function into another function? because calling it again would just mean i have to repeat the process of entering my name. basically how do i call out whats stored in cin?
Let me know if im not making sense

EDIT: Woops ignore the void name part - totally pointless sorry
Last edited on
Okay, you're not making any sense.
Why would it be any different if you called getusername in a function other than main?
If you store the return value in a variable, you can use it to do whatever you want. You can print it and/or pass it to another function if you want.
If i write

mainsName = getusername();

in another void function it would call the whole thing all over again prompting me to enter my name

im trying to use the name entered by that function later on in different functions
To do that, you can pass the name as parameter to the functions that need it.
could you show me what you mean? im not very familiar with parameters, i only know where they go but thats all.

EDIT: it took me about an hour to understand the concept of doing stuff like this, i found pretty complex so im sorry if im not totally in clue
Last edited on
Parameters are just variables that you 'give' to the function do work with, just like initializing some variables.
1
2
3
4
5
6
7
int Stuff(int a, int b) //Stuff is a function that takes two integers as parameters, and returns an intger
	//Do some random math
	a += b;
	a *= 7;
	b -= a;
	return(a / b);
}


When you 'call' the function, you 'pass' or 'give' it the values that the paremeters will be set to:
1
2
3
int SomeVariable = Stuff(2, 7); //This is a function call
int AnotherVar = Stuff(SomeVariable, 5); //You can pass variables too
int MyVar = Stuff(2, Stuff(SomeVariable, AnotherVar)); //And you can pass the result of functions too, it's all the same 


You should read this: http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
okay i tried messing around with parameters, here is something really simple but its not working.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

void farewell();

int main()
{
	const string vlad= "vlad";

	farewell( vlad );

}

void farewell(const string vlad)
{
	cout<<"Testing"<< vlad <<"....testing";
}
void farewell();

Your prototype must match the actual definition.
I'm not sure what you mean
Change void farewell();
to
void farewell(const string vlad);
so that it matches line 16.
oh okay that worked thanks! though what i was taught was that functions.. Declare, Deploy and Define. so i didn't think I'd need to do anything at the declare portion except let the computer know to look for it... that was how i thought about it, glad you helped thanks
Note that it should be const string& vlad
As it is now, you're still passing by value, which is not desirable.

so i didn't think I'd need to do anything at the declare portion except let the computer know to look for it...

A function is identified by its signature. And that's not just the name, but also the parameters it takes.
Last edited on
hmmm you said a const needs to be there too? but it worked perfectly fine withought one
I suspect C++ compiler will only wait for you to write statement to change the contents of vlad before complaining.

E.g
void farewell(const string& vlad) {
// as long as here got statements that attempt to change vlad will cause compile error

cout << vlad << "\n"; //ok

vlad += " test 123 "; //NOT ok compiler complain

}

The reason you have to give parameters types in prototypes is for overloading:
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
short Lowest(short, short); //The names of the parameters are not needed in the prototype
long Lowest(long, long); //Notice this function has the same name,
//but since it has different return and parameter types, it is "overloading"

//stuff

short Lowest(short a, short a)
{
	if(a > b)
	{
		return(b);
	}
	return(a); //in this case it will work like an else
	//because if the return(b); didn't stop the function then this will return a instead
}

short Lowest(long a, long b)
{
	if(a > b)
	{
		return(b);
	}
	return(a);
	cout << "This is the long function" << endl;
}


1
2
3
4
5
6
7
8
9
10
11
int main()
{
	short A;
	short B;
	long C;
	long D;
	cin >> A >> B >> C >> D;

	short E = Lowest(A, B); //Calls the function that does shorts
	long F = Lowest(C, D); //Calls the function that does longs, which prints that it is being called
}
Last edited on
Sorry too be back here but im trying to just get the name with a function and echo the name back with another function: here is what i have so far:
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>

using namespace std;


string getusername();
string username(string name);

int main()
{
	string mainName;
	mainName=getusername();

	username(string name);


	cin.ignore();
}

string getusername()
{
	string name;
	
	cout<<"Please enter your name: ";
	cin>>name;


	return name;
}

string username(string name)
{
	cout<<"Thank you, "<<name;

}



the error sais something about me not Declaring the function correctly inside the main
Last edited on
Take a look at this function call -> username(string name);

Then, take a look at this function call -> foo(x); // second call ,

found here: http://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/

I believe you can fix it on your own now ;)
So is my parameter wrong? because I remember a bit back up this post i had a small parameter mistake because I didnt fully write out const string parameters. I read the whole page you linked me twice.

Im still attempting this. But Im really not sure where to go. Sorry if im such a bad...
Your function getusername() returns string name places it in mainName. Name only exists in the function but is placed in mainName. So on line 15 the function doesn't know where string name is. Oh, and you don't have to define what type the parameter your passing is (on line 15). Also, you can just have username return void as you are just writing to the screen.

Ok, I'll try to explain better. If you had a variable of type 'int', named 'x' and you wanted to pass it to a function, you would do something like this:

1
2
3
4
5
int x; //variable declaration

//... assign some value to x

foo(x); // function call 

In general, if you had a variable of type 'SomeType', named 'var_name' and you wanted to pass it to a function, you would do something like this:

1
2
3
4
5
SomeType var_name;

//... assign some value to var_name

foo(var_name); //function call 

Think for a while, what's your 'SomeType' here? And what's your 'var_name'?
Last edited on
Pages: 12