r/learncpp • u/gopherhole1 • Apr 26 '21
I finished the first chapter of C++ Crash Course by no starch press, So im kinda a pro now
hows my code
#include <cstdio>
int sum(int num1, int num2) {
int result;
result = (num1 + num2);
return result;
}
int absolute_value(int x) {
int y;
if (x >= 0) {
y = x;
}else if (x < 0) {
y = (x * -1);
}
return y;
}
int step_function(int x) {
int result = 0;
if (x < 0) {
result -= 1;
}else if (x > 0) {
result += 1;
}
return result;
}
int main() {
int value1 = step_function(100);
int value2 = step_function(0);
int value3 = step_function(-10);
printf("Value1 %d\n", value1);
printf("Value2 %d\n", value2);
printf("Value3 %d\n", value3);
int num1 = 42; //This is a comment
int result1 = step_function(num1);
int num2 = 0;
int result2 = step_function(num2);
int num3 = -32767;
int result3 = step_function(num3);
printf("Num1: %d, Step: %d\n", num1, result1);
printf("Num2: %d, Step: %d\n", num2, result2);
printf("Num3: %d, Step: %d\n", num3, result3);
/*
* This is a
* multiple line
* comment
*/
int my_num1 = -10;
int my_num2 = 10;
int my_num3 = 0;
printf("Absolute Value of %d is %d.\n", my_num1, absolute_value(my_num1));
printf("Absolute Value of %d is %d.\n", my_num2, absolute_value(my_num2));
printf("Absolute Value of %d is %d.\n", my_num3, absolute_value(my_num3));
int sum_num1 = 5;
int sum_num2 = 2;
int some_sum = sum(sum_num1,sum_num2);
printf("The sum of %d and %d is %d\n", sum_num1, sum_num2, some_sum);
return 0;
}I tried to do this at first
int absolute_value(int x) {
int y;
if (x >= 0) {
return x;
}else if (x < 0) {
y = (x * -1);
return y
}
}but the compiler hated me, I queried it on a search engine and it said something about the compiler not knowing if its returning a value, and the search result wanted me to put an else clause, but I didnt want an else, so I did the first thing with the y above
6
Upvotes
3
u/JZSNooB Apr 27 '21
looks like C
1
u/JasburyCS Apr 26 '21 edited Apr 26 '21
Why do you not want an else clause? The compiler sees two conditions and is wondering what gets returned if none of those conditions is true.
We as humans are smart enough to see that one or the other must be true. But the compiler isn’t. Which is kinda the whole point of using just an ‘else’.
You could also use a dummy return value, but I don’t know why you would want to do that when an ‘else’ works fine
The code below (the one used in your first example) isn’t “good practice” either. The return statement silenced the compiler error. But the compiler still doesn’t know which if statements will be entered. This means, in the compilers viewpoint, it’s possible that none of the if statements will be entered. And then if we return y, what is its value? Undefined because we never initialized y! This will give warnings on some compilers and settings:
One final option is to forgo the else completely. If the if isn’t entered, then we can carry out the negation operation and return. Also note that we don’t need a variable y at all.
Just know that with multiple ‘if’ statements, the compiler will never know for sure which ones are entered if any. That happens at runtime. Hope this helps!