Hello I recently got into C++ about a month or two ago and am extremely excited about learning. The only problem I have is that the book I am going through right now has me stumped on functions. I have read the chapter through at least 6 times and then every web article about functions I could find. Although I enjoy programming I easily get distracted to go play Minecraft or something. I try to stay on track by coding programs I know I can do but I am sure I will get nowhere that way. Anyone have some suggestions on how to stay focused and get over functions? The thing that confuses me the most is parameters of the functions. But also ideas on how to stay on track would help. Thanks and have a good evening.
If you want to stay motivated just keep writing programs and trying to make them more and more difficult as you go. Also if you are having trouble with functions post some of your code and I will be glad to take a look and help you out.
im new to. but i know how to use functions and you can to. :)
parameters are also called arguments by the way. and functions are also called methods. anyways......here's some code.
the reason why is because when i build this function i promised it that every time I used this function I would give it some information that it needed to work properly. In this instance it is was 2 integers. "x" , "y".
I can not give it a bool type or char type. it has got to be 2 integers.
next comes the brackets. { } i code something in these brackets that will make use of the parameters that the function's user has given it.
i can use these variables for anything inside these brackets { } !!!!
now, once you have finally finished coding inside your brackets you have made one more promise to your function when you made it. You promised that when you was done running your code inside your function that you would return an integer back to it.
int add
so you say return x+y;
or whatever as long as you return an integer!!!
so in a summary of all this.....
int add (int x, int y)
you send the function information in the () brackets.
and you receive information with the return statement. And THIS FUNCTION WANTS AN int.
once you grasp this you can save the information that your function has returned inside a variable of the same type as your function. like this.
That honestly helped me soooo much! And so if I had somryhing like
int example(int a){
Random stuff
}
int main(){
int x;
Cin >> x;
Example //this is me calling the function :P laziy
}
It would place the x that we took from the user into our function!!! I get it now thanks!
Clarification: Arguments and parameters are not the same. A parameter is the variable in parenthesis, while the argument is the value passed to the parameter. So the parameter's value is the argument. Arguments may change from one call to the next, but the parameters are the same.