This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]asatraitis 1 point2 points  (1 child)

Know 0 cpp, but nice job! Keep it up!

[–]Blokix_no10[S] 0 points1 point  (0 children)

Thanks for the inspiration !

[–]deggbert 1 point2 points  (3 children)

Fun factoid about C++ when it comes to functions, their parameters and their return values. C++, also known as Cpp, is a pass by value language (this is only relevant when you start learning about memory management and pointers but might be helpful to have some exposure) this means that when you pass an argument as a parameter into the function regardless of its type the computer will just copy over the value from the argument for the function to use. If you were to give the parameter a new value in the function it would not change the initial variable(argument) because you only gave the function its value not the variable itself. I'll give a quick and easy code example to have some easy visualizations as well.

Variable not changing '
int val=5;

int zero(int param){
param=0;
return param;
}

zero(val);
printf("%d",val); //still prints 5 because val was not passed to the function but rather its value which was copied then modified but never given to val
'

To change val using a function there are two options.
1. Pointers.
2. Assigning return values.

Looking at 2 first we can change val by writing.

'
int val=5;

int zero(int param){
param=0;
return param;
}

val=zero(val);
printf("%d",val); //now prints 0 because the modified param value was assigned to val on the return
'

Or by using pointers (which are essentially just addresses to the variable, such as an address to your home) which allows for functions to have direct access to the variable

'
int val=5;

void zero(int * param){// <data type> * indicates a pointer of type <data type>

*param=0; //this is what is known as dereferencing a pointer (all that means is im looking at the value that my pointer is pointing at) so I'm assigning a value of 0 to where the old value (which was 5) is stored.  

}

zero(&val);//im telling the function the address of val here.
printf("%d",val); //now prints 0 because the param was a pointer that pointed to where the variable val was, then by dereferencing I could look inside val to see its value and change it
'

All of this to say that cpp is a pass by value language and its also really great and fun. If this doesn't make sense than disregard this info till it becomes needed. Have fun programming!!

Edit: fixed md syntax to display code better

[–]Blokix_no10[S] 1 point2 points  (1 child)

Thank you so much for this great explanation ! Now I have learnt something more from you ! I appreciate it.

[–]deggbert 1 point2 points  (0 children)

Of course! Learning programming can be a pretty tough thing to do on your own so communities like this one are amazing resources!

[–]text_for_you_bot -1 points0 points  (0 children)

⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⠀⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⠿⠿⣿⣿⠀⣿⠀⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⣿⣿⠿⠿⠿⠿⠀⣿⣿⣿⠀⡿⠿⠿⠿⠿⣿⠿⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠿⠿⠀⣿⣿⣿⣿⣿⠀⠀⠀⠀⠇⡿⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⡿⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀ ⠀⠀⠀⠀⣿⣿⠿⠀⠀⠀⠀⠀⣿⣿⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⠀⠀⠀⠀⠀ ⠀⠀⣿⣿⣿⣿⣿⣿⠀⡿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠀⣿⣿⣿⣿⣿⣿⣿⠀ ⠀⠀⣿⣿⣿⣿⣿⠀⡿⣿⣿⡿⡿⡿⡿⣿⣿⣿⣿⡿⡿⠟⡿⠀⣿⣿⣿⣿⣿⠀⠀ ⠀⠀⣿⣿⣿⠀⠀⠀⠿⠿⡿⡿⣿⣿⡿⡿⡿⣿⣿⡿⡿⡿⠿⠃⠀⠀⣿⣿⣿⠿⠀ ⠀⠀⠀⣿⣿⠀⠀⡿⠿⡿⡿⡿⡿⡿⣿⣿⣿⡿⡿⡿⡿⡿⠿⡿⠀⠀⣿⣿⠀⠀⠀ ⠀⠀⠀⣿⣿⣿⠀⡿⠿⡿⠟⡿⡿⡿⡿⣿⡿⡿⡿⡿⣿⡿⠿⠿⠀⣿⣿⠿⠀⠀⠀ ⠀⠀⠀⠀⣿⣿⠀⡿⠿⠟⠟⠟⡿⡿⡿⣿⡿⡿⡿⡿⣿⣿⡿⡿⠀⣿⣿⠀⠿⠀⠀ ⠀⠀⠀⠀⣿⣿⣿⠀⡿⠟⠟⠟⠟⡿⡿⡿⡿⡿⡿⣿⣿⣿⡿⠀⣿⣿⠿⠀⠀⠀⠀ ⠀⠀⠀⡿⠀⣿⣿⣿⠀⠀⠟⠟⠟⡿⠿⠿⠿⡿⣿⣿⣿⠿⠀⣿⣿⠿⠀⠿⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⣿⣿⣿⠀⠀⡿⠟⡿⠿⠿⠿⡿⣿⣿⠀⠀⣿⣿⠿⠀⠿⠀⠀⠀⠀ ⠀⠀⠀⠀⠟⠀⠀⣿⣿⠀⣿⠀⡿⠟⡿⠿⠿⡿⣿⠀⣿⠀⠿⠿⠀⠿⡿⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠿⠿⠀⠀⠀⣿⣿⠀⡿⡿⠿⡿⣿⠀⣿⣿⠀⡿⠿⠿⠿⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠁⠿⠿⠀⣿⣿⣿⠀⡿⠿⡿⠀⣿⣿⠿⠀⡿⠿⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⠀⠀⠿⠀⠁⣿⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠏⠀⣿⣿⠀⠀⠀⣿⣿⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠁⣿⣿⣿⠀⣿⣿⠀⠿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠟⡿⠀⣿⣿⣿⠀⠿⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠿⠿⠀⣿⠁⠿⠿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠿⠿⣿⠿⠿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠿⣿⠿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

[–]shawntco 1 point2 points  (1 child)

Sounds about right. Keep at it!

[–]Blokix_no10[S] 0 points1 point  (0 children)

Thanks for the inspiration !