Function calling

How do you call a function?
Also do you have to define the functions someplace?
That doesn't help me.
Probably doesn't help you because you did not read it. Click on the control structure, functions 1 ( i ) , functions 2 ( ii ).
I did. I don't get i.
Perhaps programming isn't for you then. I've read a lot of your posts and they are asking the basic questions you should already know the answer too, or be able to follow a tutorial. You seem clearly unable to follow a simple tutorial.

I'd suggest heading to www.learncpp.com and read those tutorials. If you cannot figure them out, go outside and find another hobby because programming isn't for you.
My function is

1
2
3
4
   int knight () {
    cout << "You are in a tavern. Leave? (Y/N)";
    getline(cin, answer);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using std::cout;  //A preferred alternative to using namespace std;
using std::endl;

void funky();   //Function declaration; a promise to the compiler that 
                //you've defined the function somewhere else

int main(){
   funky();  //Function call
   return 0;
}

void funky(){   //Function definition
   cout << "Hello human who does not understand how to call functions." << endl;
}


Still confusing?

Edit:
Remember that variables only exist within their respective function blocks ("{}"), so answer will remain undefined in your knight function.... Unless you made it a global variable....
Last edited on
No. Thank you.
Reporting Cometlove1 for spamming, refusing to read answers or tutorials
Thanks a ton.
No problems. Clearly you didn't read the "Read before posting" thread.
http://www.cplusplus.com/forum/beginner/1/
why does it show his report as reported for me lol? When ever I report someone it deletes their post...
On another note
@Daleth
I think if the using std::cout is much better than using namespace std...
but, I think if you are going to do that you should atleast put it in the function they are actually being used in
ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    using std::cout;
    using std::endl;
    cout << "Hello world!" << endl;
}
//vs

#include <iostream>
using std::cout;
using std::endl;
int main()
{
    cout << "Hello world!" << endl;
}


Also I believe line 6 is the prototype. Could be wrong though.
Yes, it is a prototype.
Also, yes, secluding the using keywords closer to where they are used is a better choice similar to declaring variables closer to where they are used instead at the beginning.
However, if you are using the namespace members in multiple places such as std::cout, std::cin in an interface class or the containers in many classes, listing the "using" at the top might be better than retyping "using" in every function.
Ok, let's just try to explain this one step at a time.

The simplest function example I can give you
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>

void printHello(){
	std::cout << "Hello";
}

int main()
{
	printHello();
	return 0;
}


The reason that function used void was because we didn't want to return anything. It would be a waste of resources making that function an int, a float, a string, etc.

In this case the compiler sees the function at the top, get's what it's supposed to do with it, then in int main() sees it being called and refers to the instructions under that function. Whenever you call a function you simply type the name of the function printHello then parameters (). In this case we don't have anything we are passing into the parameters, so it's just printHello();

Prototyping a function is the same way as far as how you write it out except for your parameters printMessage(std::string message); Then somewhere else we can actually define the function (which is another difference, don't try to define your function in another function, either define it globally or in a class)

1
2
3
void printMessage(std::string message){
	std::cout << message << std::endl;
}


Entire program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<string>
void printMessage(std::string message);

int main()
{
	printMessage("Here's a message");
	return 0;
}


void printMessage(std::string message){
	std::cout << message << std::endl;
}


The prototype at the top allows the function to be defined after int main() rather than before. However, this is just a simple example of the power of prototyping. You'll use it A LOT more once you start handling header files. The (std::string message) is like a placeholder saying "When this function is called, a string will be passed in here"

If you don't understand just PM rather than drive everyone crazy. Programming isn't just something you spend 15 minutes here and there picking up stuff. It can be a very grueling activity and takes a lot of invested time to get good at. (I've been at it for about 6 months and honestly still feel like a complete noob compared to the majority of people on here, despite spending A LOT of time programming) There's no easy around this. I think a good programmer will read a lot, think a lot, have a lot of patience, among other things. If this isn't for you or you're one of those people getting into programming because you heard it pays good money, you need reconsider your career choice possibly.
Topic archived. No new replies allowed.