So confused when programming

closed account (iAk3T05o)
I get so confused when programming anything other than basic stuff.

Most especially when using functions.
Is it because i'm going too fast or something else.
closed account (Dy7SLyTq)
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
closed account (iAk3T05o)
It just seems so confusing and demotivating and i don't like not knowing what to do next
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.

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
#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
}

http://coliru.stacked-crooked.com/a/03b2ace3e16077b6
closed account (Dy7SLyTq)
of course a better way would have been return a + b.
An even better way would have been not to write the function at all.

What else is new?
closed account (Dy7SLyTq)
well its good to know. it could turn into some_class::operator+
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.
Last edited on
closed account (iAk3T05o)
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.
Last edited on
This was a major turning point for me, and hopefully you have the same experience.
closed account (iAk3T05o)
What it's suppose to do is display good if c is equal to a and better if c is equal to better. The 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
#include <iostream>

int numbers (int a, int b)
{
if(a)
{
std::cout << "\n Good \n";
return a;
}
else if (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.
If you want a function that:
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";
    }
    else if (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.
Last edited on
What it's suppose to do is display good if c is equal to a and better if c is equal to better.

But your numbers funtion doesn't compare c to anything. It doesn't even know what c is, because you're not passing it in.

All it's doing is printing "Good" is a is non-zero, and otherwise printing "Better" if b is non-zero.

EDIT: And at line 20, you have:

a,b = numbers (10, 20)

I don't know what you think the comma operator does in C++, but this line is almost certainly not doing what you intended.
Last edited on
closed account (iAk3T05o)
Thanks kevin.
What of char functions. Everytime i try it, it displays nothing.
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.
Last edited on
closed account (iAk3T05o)
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.

Cheers!
closed account (j3Rz8vqX)
Think of functions as code written elsewhere.
1
2
3
4
5
6
7
8
9
10
int main()
{
    //These
    //are
    //comments
    //written
    //in
    //main
    return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void do_stuff()
{
    //These
    //are
    //comments
    //written
    //in
    //do_stuff
}
int main()
{
    void do_stuff();//Do the stuff elsewhere;
    return 0;
}

That can be Reused.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.
Topic archived. No new replies allowed.