functions

Pages: 12
Wohooo! that was super awsome badassery! thank you. one question.
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;


string getusername();
void username(string mainName);

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

	username(mainName);


	cin.ignore();
	cin.ignore();
}

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


	return name;
}

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

}


why wont it let it be string mainName inside of int main I mean it seems like im just defining it more than i need to but it refuses to take it like that. Since it takes string mainName everywhere else it just seems weird.


@m4ster r0shi - thank you r0shi, sorry im so stupid. Even your second explanation i had to re-read twice. But I see what you are saying that basically instead of name i should have been calling mainName however, this relates to my above question too, why does it matter what my SomeType is if c++ doesnt even want me to put in a type?
Last edited on
I'm not quite sure what you mean...

why wont it let it be string mainName inside of int main


string mainName is inside of int main()

I mean it seems like im just defining it more than i need to but it refuses to take it like that.


You only defined it once as far as I can tell.

If you can reword your question a tad I'll hopefully be able to answer your question.
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;


string getusername();
void username(string mainName);

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

	username(string mainName);


	cin.ignore();
	cin.ignore();
}

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


	return name;
}

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

}


if i add that bold part in there it wont allow it to compile

EDIT: now underlined
Last edited on
Oh, I see what you mean. I don't know the particular reasons but the function doesn't really need to know the type (at least not in the parameter field), just the variable name. If you think of/read up on function overloading, you will see that you simply name the parameter and the function used is determined by the variable type even though the function names are the same. A quick example using your code:

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 username(string mainName);
void username(int mainName); //same function name yet different parameter type

int main()
{
	string mainName;
	int overload; //what I'm using as an example

	mainName=getusername();
	overload = 5; //giving it a value

	username(mainName);
        username(overload); //same function name but using a number?! GASP

	cin.ignore();
	cin.ignore();
}

string getusername()
{
	string name;

	cout<<"Please enter your name: ";
	cin>>name;


	return name;
}

void username(string mainName)
{
	cout<<"Thank you, "<<mainName;
}

void username(int mainName)
{
    cout << "\nYou entered a number yet used the same function name\n"; //this will print out after
                                                                        //the other username func. finishes                          
}        



So simply designate the variable name and leave out the type.
When you write a function, you must provide a declaration and a definition for it. In these, you must specify the type of the arguments and a name for every argument (though that last one can be omitted in declarations). When you call your function, you just have to pass something that agrees with your function's parameter list.

Example:

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
#include <iostream>
using namespace std;

//function declaration
void foo(int x);

//this would also work
//void foo(int);

int main()
{
    int y;

    y=5;

    //function call
    //y is of type int,
    //we are ok
    foo(y);

    cin.get();
    return 0;
}

//function definition
//we need names for the paremeters
//here. how else could we refer to
//them inside the function body?
void foo(int z)
{
    cout << "your variable's";
    cout << " value is: ";
    cout << z << endl;
}

vlad61 wrote:
it seems like im just defining it more than i need to but it refuses to take it like that

That would be this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

string getusername();
void username(string mainName);

int main()
{
    string mainName;
    mainName=getusername();
    
    void username(string mainName);
    
    username(mainName);
    
    cin.ignore();
    cin.ignore();
}

//definitions here
//... 

And it compiles fine.
thank you so much guys! this was very informative. BTW m4ster r0shi I tried "over-defining" it like you said and it actually just skipped that whole line. any ideas?

EDIT: but it DID compile fine, just skipped the name echo process.

EDIT: OH and ive been curious. what is foo?? why was it used so many times for examples? I was thinking maybe it stands for function but its strange.
Last edited on
vlad61 wrote:
I tried "over-defining" it like you said and it actually just skipped that whole line.

It didn't "skip" it. A function declaration isn't a function call. Don't expect it to print anything. A function declaration is used to inform the compiler about the name of the function and its return and parameter types. Without it, the compiler can't know if an identifier (a word) is a function or not. Take a look at this:

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
#include <iostream>
using namespace std;

//function declaration 1
//void foo(int x);

int main()
{
    int y;

    //function declaration 2
    //void foo(int x);

    y=5;

    //function call
    foo(y);

    cin.get();
    return 0;
}

//function definition
void foo(int z)
{
    cout << z << endl;
}

This won't compile. The compiler complains about this-> foo(y);, because it doesn't know that 'foo' is a function (because there's no declaration of 'foo'). Uncommenting either the first or the second (or both) declaration(s) will do the trick. The rule is that you must always declare a function before you call it.

Something else. You can use the definition itself as a declaration, but you have to put it before every function call. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

//function
//declaration
//and definition
void foo(int a)
{
    cout << a << endl;
}

int main()
{
    int b=10;

    //function call
    foo(b);

    cin.get();
    return 0;
}

However, this is not a good idea. This explains why:

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
#include <iostream>
using namespace std;

void foo(int a)
{
    if (a<=0) return;

    cout << "foo: ";
    cout << a << endl;

    bar(a-1); //compiler says: "huh? bar? what bar?"
}

void bar(int b)
{
    if (b<=0) return;

    cout << "bar: ";
    cout << b << endl;

    foo(b-1); //ok, foo was declared above
}

int main()
{
    int x=5;

    foo(x); //ok, foo was declared above

    cout << endl;

    bar(x); //ok, bar was declared above

    cin.get();
    return 0;
}

You can easily get around this by putting this -> int bar(int); above foo's declaration-definition of course, but this is ugly. It's better to have function declarations above main and function definitions below it.

EDIT:

vlad61 wrote:
OH and ive been curious. what is foo??

http://en.wikipedia.org/wiki/Foobar
Last edited on
Topic archived. No new replies allowed.
Pages: 12