Message from @Tero
Discord ID: 620831451272970240
I know 20 year old electronics graduates that won't install an altium product if it pops up windows utc
lol
how do i avoid getting scope fucked in c++
if i have an if statement
to store a smaller or larger int
but then i want to use those variables globally
maybe extern keyword could help?
not sure, I've only done arduino C++, but that's how I usually do it
BJ's PPP has some prank questions, like it gives questions that require knowledge of scope and list sorting before it teaches you it
avoid global variables for most purposes
globals are fine as long as properly named and it has few setters (ideally, it's just set in one place, once)
having used threads a bit it's abundantly possible to make the kinds of errors people supposedly cause with globals by referencing an object or variable with multiple threads
`Although threads seem to be a small step from sequential computation, in fact, they represent a huge step. They discard the most essential and appealing properties of sequential computation: understandability, predictability, and determinism.`
(this is probably where functional fans come in and tell everyone about how having any shared state is bad)
for some reason this program returns x as 25 million times its number
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open(){ char ch; cin>>ch; }
//for some reason, the square function returns 25 million
int square(int x)
{
x +=x;
cout << x;
}
int main()
{
int i = 0;
cout << "Enter a value.\n\n";
cin >> i;
cout << square(i);
}
seeing all these double >> around are you sure you're not bitshifting something?
@Tero that's how you print in c++
And take input
In C++ the '>>' and '<<' indicator data flows with iostream
ok
why's it returning wrong results if not bitshifting then
Why use '+=' here @What Would Jack Conte Do? ?
I don't get the point of the cout in the function
Hmm, that could be something, nesting Couts in the return sequence
Why not ```return x;```?
He's already imported the cmath library, so why didn't he use the pow() function?
both good questions
so apparently the int-returning function square with no return statement results in undefined behaviour
sure do love C/C++
Okay so definitely swap the function to
```int square(int x)
{
x *=x;
return x;
}```
TIL this is a thing https://www.onlinegdb.com/online_c++_compiler
Oh yeah that's super useful
Saves me the trouble of having to unpack Visual Studio's doodads
Saves me the trouble of SSHing
well its an excercise to replicate multiplication without multiplying, like what was done in ancient times
kinda stupid tbh