move this to the lounge. and dont worry about it. i was self taught, and got confused all of the time. it does have its benefits, but if i had discovered this site it would have been a lot easier. if you ever have any questions feel free to ask
Think of a function as an expression that is evaluated. For instance, if we have: int i ; std::cin >> i ;
i + 32 is an expression; the result of this expression is an int
Likewise, if we have a function int foo( int a, int b ) ;
Like i + 32, foo( i, 32) too is an expression; the result of this expression is an int
When the function foo() is called, copies of two integers are passed to it. The function does something with these to arguments, evaluates the result of the expression, and gives back the evaluated result via a return statement.
#include <iostream>
int foo( int, int ) ; // declares that foo is a function
// it takes two parameters of type int and the result of evaluation is an int.
int main()
{
int i ;
std::cin >> i ; // say, 57
int k = i+32 ; // k is initialized with the result of evaluation of i+32
std::cout << k << '\n' ;
int l = foo( i, 32 ) ; // l is initialized with the result of evaluation of foo( i, 32 )
std::cout << l << '\n' ;
// use the expression anonymously
std::cout << i + 56 << '\n' ; // print the result of evaluation of i + 56
// use the expression anonymously
std::cout << foo( i, 56 ) << '\n' ; // result of evaluation of foo( i, 56 )
}
// define foo
int foo( int a, int b ) // a and b stand for the two integers that are pased too it
{
int result = a + b ; // evaluate the result
return result ; // and return it to the caller
}
While it's true that a function is not really necessary to perform basic operations like adding two numbers, it helps demonstrate the use of functions for beginners. I know that Nathan2222 recently got his Jumping into C++ book, so reading up on basic function usage and some of the other basic concepts should help him a lot.
As DTSCode said earlier, all of us experience confusion at one point or another. Sometimes it can be demotivating, but having forums like this to ask questions in rather than relying on textbooks alone can be helpful. I would recommend starting with simple stuff, like Tic-Tac-Toe, or perhaps an even simpler program, as it helps develop experience with how the language works. Sometimes I write programs to perform various things I already know how to do, but when writing them I explore different options and gain a greater understanding of how it works. The beautiful part is that instead of being completely useless, the code can potentially be used in other programs I make in the future, which makes it more than just a learning exercise.
It was actually when i was making my tictactoe game that i realised how confusing functions were to me.
I think i have to start learning c++ from the beginning.
#include <iostream>
int numbers (int a, int b)
{
if(a)
{
std::cout << "\n Good \n";
return a;
}
elseif (b)
{
std::cout << "\n Better \n";
return b;
}
int main()
{
int a = 0;
int b = 0;
a,b = numbers (10, 20)
int c = 0;
std::cout << "Enter a number btw 0 and 20: ";
std::cin >> c;
if (c == a|| c == b)
{
std::cout << numbers (a,b)
}
else
{
std::cout << "\n Hmm\n";
}
return 0;
}
I know it can be done with 2 void functions but i wanted to try if/else statements in strings. What are the mistakes?
Thanks.
display good if c is equal to a and better if c is equal to better
Then this function will need three inputs. func(int a, int b, int c)
The function also needs a return type. I would choose void because this function will not be returning any data of any type. The function is only going to print a result to the screen. Printing functions are typically void because the output of the function is "returned" directly to the screen. Now it could be possible to get fancy and have this print function return an integer error code. The error code would let the section of code that called/ran the function know whether an error occurred during a simple output operation. But for the sake of keeping it simple let us just stick with void since no data is returned. void func(int a, int b, int c)
I think you know enough to piece together the code inside the function, but I would still like to provide an example. Just stop reading if you want to try it before checking my example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void func(int a, int b, int c)
{
if (a == c)
{
std::cout << "\n Good \n";
}
elseif (b == c)
{
std::cout << "\n Better \n";
}
else
{
std::cout << "\n Not good \n";
}
}
This could be easily modified to return that integer you were returning in your function.
There are no such things as "void functions", "char functions", "int functions", "bool functions", etc. - there are only "functions", and you specify what kind of value the function returns, if any, when you declare and define it.
Try instead writing "char-returning function" or "function returning char". It is important to understand functions correctly and when you don't describe them properly it leads people to believe you don't understand them.
Ok.
A char returning function. I used a char returning to quit a program if the user inputs 'y' and thanks to one article on the internet, i also understand bool type functions and gained a better understanding of for loops.
I still need practice.
You still don't understand functions, because there's not such thing as "X type functions". Try to read some tutorials/books, and experiment with code. Look what happens when you do X, and what when you do Y, and figure out why. You won't learn by only reading.
You should understand functions as an idea, not as a thing("X type function"), because later it may be hard for you to understand templates.
As for headache programming gives, I don't know how old are you - if you're less then 15 y/o, age may be the reason - but programming isn't really easy - if it was, everybody would be doing it :) If you like it, learn, try new things, and you will move on.
void do_stuff()
{
//These
//are
//comments
//written
//in
//do_stuff
}
int main()
{
void do_stuff();//Do the stuff elsewhere;
void do_stuff();//Do the stuff elsewhere, again x 1;
void do_stuff();//Do the stuff elsewhere, again x 2;
return 0;
}
And possibly more:
2)Recursion
3)Templates
As MatthewRock said:
You should understand functions as an idea, not as a thing("X type function")
Because it really isn't anything... just a door to code written elsewhere.
What it contains is important, and possible manipulation of it with patterns and techniques - which you should discover when necessary.