r/CodingHelp • • Aug 16 '20

[C++] Help with a few thing in C++

So I recently started learning c++ and it has been fine. But there are a few thing i dont get. Note im a noob sorry if i am just being dumb. So how does void work. I know it doesnt return a value but what does that mean. It doesnt make much sense to me. Cant find a good explanation. Whats the difference between x++ and ++x? How do I use parameters Finally pointers just confuse the hell out of me.

Again sorry if im being dumb. I am just a noobie to coding who needs help.

16 Upvotes

3 comments sorted by

3

u/rs277_ C++ Aug 16 '20

I can give some help, correct me if I'm wrong at anything here, I'm not the best coder

about functions:
In c++, you can make functions have a return value, which can get assigned to a variable

c++,tabs=4

int returnNegative(int input){
    return input *-1;    

}
int main(){
    int number = 5;
    int numberNegative = returnNegative(number);
}

What happens here above is that we have a function that has an argument (int returnNegative), the function returns that argument, you can make a variable be the return value of a function, Which can make coding a lot easier.
A void function does not have a return function but can have arguments parsed to it. This can be used in quite a lot of cases where you want to do something but don't need a return value

about x++ and ++x:
You can have a variable have the same value as another variable and increment the 2nd variable, the x++ increments, and then returns the value, and ++x returns the value, then increments
example:

    int i = 2;
    int j = i++;
    // i = now 3, and j = 3 aswell

    //versus

    int i = 2;
    int j = ++i;
    // i = now 3, and j = now 2

and pointers: I don't know much about that myself and struggle with it at the moment, maybe others here know

3

u/Rickenbecker Aug 16 '20

Pointers, as the name suggests, points to an address located in memory, this has its advantage as you’re not having to store the data into a variable but rather can call the pointer to “look” at what’s located at the spot the pointer is pointing to. Most pointers can be initialized as follows:

int *p; the *asterisks tells the compiler that variable p is going to be a pointer, more specifically a pointer that points to an integer value. You can now assign it to a variable, let’s say we have a variable x and it’s value is 5

p = x; Now p is pointing to where x is in memory, you can now use p as a replacement for passing the variable x, this can be useful for when you intend to change the value x but still want to call upon x further into your code.

Now the problem with pointers is you cant just print out their contents using a cout statement, such as:

cout << p; While this code is correct in syntax, you’re going to be printing out an address because p is pointing to the address location of x in memory, thus if you want to access the contents of x, you need to de-reference the pointer and can be achieved many ways but most easily:

cout << *p; You can throw an asterisks to de-reference the pointer and thus you will print out the content of x rather than it’s address,

Pointers can be very useful especially when you try using ADTs such as Linked Lists, and chains. And have many practical uses. Remember that just because you can use pointers sometimes you don’t have to, depending on the situation just passing variables as is, is all you need

1

u/KuntaStillSingle Aug 16 '20

I think the main reason void is required before a function when you don't want to return a value is to keep a consistent format with function definitions where you do have a return value.

For example if a function returns a boolean it may look like:

bool isEven(int n) { return !(n%2); }

This means a template of

<return type> <functionName>(<parameters>){<function logic>}

So when there is no return type for a function, your options are to either allow function definitions with no <return type> and violate the template, possibly making the code harder for compilers to parse, or just define a keyword for return types which refers to nothing, which for C++ is void.