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.
#include <iostream>
#include <string>
usingnamespace 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
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.
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
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
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
}
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'?