Variables and Scope - CS50 Shorts

preview_player
Показать описание
***

This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.

***

HOW TO SUBSCRIBE

HOW TO TAKE CS50

HOW TO JOIN CS50 COMMUNITIES

HOW TO FOLLOW DAVID J. MALAN

***

CS50 SHOP

***

LICENSE

CC BY-NC-SA 4.0
Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License

David J. Malan
Рекомендации по теме
Комментарии
Автор

This is too good to be true. I'm taking this course on edx cs50x but i prefer to watch it using their youtube links to read comments after every video. This series is unbelievably good... <3 Thank you team cs50

ashish
Автор

If anybody didn't catch what's going on here 3:55-4:25
Look and check =)

#include <cs50.h>
#include <stdio.h>

int triple(int x);

int main(void)
{
int foo = 4;
printf("foo is %i\n", foo);
// On the next line write at first just triple(foo) and then change next line as was before
foo = triple(foo);

printf("foo is %i\n", foo);
}

int triple(int x)
{
return x *= 3;
}

BuBahard
Автор

Finally someone explained why variable with the same name in the function Main has not the same value in few places!!!
Thank you so much!!! I was fighting with these problem for a month already.

maxim
Автор

Doug you are a master of fantastic explanations, tysm.

EnglishRain
Автор

Int increment is equal to 1 so when y = increment(x) in main it adds the local variable of x to that of increment which would be 1+1 = 2. Therefore y would equal 2

chazturd
Автор

Why in this case we didn't need to tell compiler about triple()? I mean we always supposed to write the prototype before the main function?

ritaahmed
Автор

Don't use foo as a variable name in a beginner course, it's not helpful, and confusing. Instead use local_var or global_var to better illustrate what is occuring. Also the math analogy is great, but another analogy for those not math inclined would have been helpful. I got the right answer when looking at the code but the follow up explanation confused me so much.

stephaniemiller
Автор

7:01 I really did not comprehend the explanation. If anyone can explain?

Is x getting overwritten by "y" when it says: y = increment(x) ?
And is the blue x literally the same thing as red x, because it was incremented and the answer for y ended up being 2?

paulasuzettebravo
Автор

Please watch Zamyla, she is excellent at explaining.

stantonreddy
Автор

3:45 Bad example, the code inside the function triple should be "return x * 3". It would work just as well and wouldn't confuse the fuck out of beginners who haven't been taught that the assignment operator actually evaluates to something.

Interestingly, it's possible to fix this very easily without re-recording anything, by just painting over that line with white then putting the new text over it (or just erase the entire code and replace by the new code, to not have to deal with matching the font, size and position of the modified line).

leandrog
Автор

I have tried to rebuild the function at 2:23 and it seems to miss a first declaration of tripple before int main(void), otherwise it does not know what tripple is. Do you have the same problem or am I missing something?

kuper
Автор

7:25 why xi doesn’t start from 0, so after x++, y should be 1 rather than 2?

sueg
Автор

i guess the implementation of triple in main at 4:00 is not correct !!

avvarisreedhar