Side Effects - CS50P Shorts

CS50 · Beginner ·💻 AI-Assisted Coding ·2y ago

Key Takeaways

Explains side effects in CS50P Shorts

Full Transcript

[Music] well hello one and all and Welcome to our short on side effects so we've seen that functions are these things in programming languages that take inputs and produce outputs for us now we've seen really that there are various kinds of outputs one of them is called a return value the value the function gives back to us after it finishes running but it turns out there's also a kind of output known as a side effect fact which is not the return value but anything else that gets changed while our function is running so let's take a peek at an example here here I have a program called machine. piy representing some machine with some internal emotional state notice how up at the top here I have an emoticon representing our machine's emotions at this given point in time in this case it has this particular emoticon representing something like uh sadness or forlornness and so we'll see if we can make our machine more happy as you write our program but ideally we want this machine to say something to us and so let's go ahead and write ourselves a function called say that we can then call inside of our main function here so I will underneath main go ahead and write for myself a function called say using the python keyword defa and the name for the function say and maybe say should take as input as an argument here maybe something called phrase the phrase to say and so by convention I will indent within this uh code block here to write the body of my function the code I want to run when say is called or used in my program well what should say do I think really to say something our machine should print something to the terminal and so we can do that with a python function known as print just like this and what should print print well probably the phrase in this case the phrase we gave as input to say now if we're just using print we haven't really added much functionality here uh to the say function so why don't we have the machine able to say something but then maybe append the emoticon at the end so it kind of says something with emotion here I'll go ahead and say print the phrase but then also print on a space followed by the emoticon itself up top here so notice how phrase is really a local variable it's passed as input to this function say and used with inside that function emoticon though as a global variable defined at the top of our file here is able to be accessed by any particular function in this case say included so ideally say should then say whatever phrase we have followed by a space followed by the emoticon representing our machine's emotions so why don't we try this out in the main function which will we run as we run our program here I'll click I'll say uh say maybe uh oh is anyone is anyone there question mark and so if we run our machine down below I'll say python of machine. piy hit enter and we'll see is anyone there with a little bit of a sad face so made ourselves a Sad Machine but let's go ahead and try to make it happier as we go through so maybe it could later on say something like um oh hi just like that and if I were to go ahead and run this I'll say python of machine. py hit enter and we see now two lines of text is anyone there and oh hi now there's some things to fix ideally the emoticon would be happy in the terminal down below and it says oh hi but before we go there let's first take stock of what we've done like we've actually written a program that has a side effect this is one example of a side effect printing to the terminal in this case so our function say has the side effect of printing to the terminal but what other kinds of side effects might there be well we mentioned earlier that a side effect really anything that gets changed all our function is running that is not the return value actually give back at the end of our function so maybe inside of main we could try to change the emotional state of our machine as a side effect so let's try this out here I'll clear my terminal with contrl L and then I'll go on to line six here and before we say oh high let's try maybe making the emotion a little happier in this case I'll say uh Colon D for the big smiley face and iide ideally if we were to run this program we'd see is anyone there with a bit of a sad face followed by oh hi with a bit of a happy face I'll run python of machine. piy H we seem to still have ourselves a Sad Machine so how can we improve this well I notice first here that emoticon if you can see very slightly is grayed out and this is because python by default wants us to specify that we're trying to change some Global variable inside of a function this more local scope so our side effect that we want to have happen is to change this Global variable but because this isn't always desirable python wants us to explicitly specify what we're going to do so to do this within the main function what I can do is use this keyword called Global and I can specify after global the global variable I want to make accessible and modifiable within this particular function and key thing there is it's now modified not just accessible notice how in say it was accessible to us modicon was but in main it wasn't modifiable I couldn't update this variable if I now say Global emoticon I'm able now to modify the actual value of that variable as a side effect not the return value here but just a side effect as this function runs so here again we have our Global emotional state for our machine we then have our function called main with in that function we say we're going to be able to change this Global emotional state that we called emoticon we'll first say is anyone there change the emotion and then say Oh high so let's try this out I'll go ahead and run Python machine. and there we go we've now seen another version of a side effect one that changes some Global variable now in programming this may or may not be desirable if you have a global variable that is accessed across many other functions it can be hard to debug your code if that variable gets modified across all these different functions so keep in mind when you side effects like these make sure it's good design right decision to actually make when you're writing your program but here we've seen two examples printing the terminal modifying some Global State there are others too but I'll leave that for a later time we'll see you next time

Original Description

*** This is CS50, Harvard University's introduction to the intellectual enterprises of computer science and the art of programming. *** HOW TO SUBSCRIBE http://www.youtube.com/subscription_center?add_user=cs50tv HOW TO TAKE CS50 edX: https://cs50.edx.org/ Harvard Extension School: https://cs50.harvard.edu/extension Harvard Summer School: https://cs50.harvard.edu/summer OpenCourseWare: https://cs50.harvard.edu/x HOW TO JOIN CS50 COMMUNITIES Discord: https://discord.gg/cs50 Ed: https://cs50.edx.org/ed Facebook Group: https://www.facebook.com/groups/cs50/ Faceboook Page: https://www.facebook.com/cs50/ GitHub: https://github.com/cs50 Gitter: https://gitter.im/cs50/x Instagram: https://instagram.com/cs50 LinkedIn Group: https://www.linkedin.com/groups/7437240/ LinkedIn Page: https://www.linkedin.com/school/cs50/ Medium: https://cs50.medium.com/ Quora: https://www.quora.com/topic/CS50 Reddit: https://www.reddit.com/r/cs50/ Slack: https://cs50.edx.org/slack Snapchat: https://www.snapchat.com/add/cs50 SoundCloud: https://soundcloud.com/cs50 Stack Exchange: https://cs50.stackexchange.com/ Telegram: https://t.me/cs50x Threads: https://www.threads.net/@cs50 TikTok: https://www.tiktok.com/@cs50 Twitter: https://twitter.com/cs50 Twitter Community: https://twitter.com/i/communities/1722308663522594923 YouTube: http://www.youtube.com/cs50 HOW TO FOLLOW DAVID J. MALAN Facebook: https://www.facebook.com/dmalan GitHub: https://github.com/dmalan Instagram: https://www.instagram.com/davidjmalan/ LinkedIn: https://www.linkedin.com/in/malan/ Quora: https://www.quora.com/profile/David-J-Malan Threads: https://www.threads.net/@davidjmalan TikTok: https://www.tiktok.com/@davidjmalan Twitter: https://twitter.com/davidjmalan *** CS50 SHOP https://cs50.harvardshop.com/ *** LICENSE CC BY-NC-SA 4.0 Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License https://creativecommons.org/licenses/by-nc-sa/4.0/ David J. Malan https://cs.harvard.edu/malan m
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from CS50 · CS50 · 0 of 60

← Previous Next →
1 Hello, World: Hadi Partovi
Hello, World: Hadi Partovi
CS50
2 Content Distribution and Archival in a Digital Age
Content Distribution and Archival in a Digital Age
CS50
3 CS50 2014 - Week 1
CS50 2014 - Week 1
CS50
4 CS50 2014 - Week 3
CS50 2014 - Week 3
CS50
5 CS50 2014 - Week 0, continued
CS50 2014 - Week 0, continued
CS50
6 CS50 2014 - Week 4
CS50 2014 - Week 4
CS50
7 Week 3, continued
Week 3, continued
CS50
8 Quiz 0 Review
Quiz 0 Review
CS50
9 CS50 2014 - Week 3, continued
CS50 2014 - Week 3, continued
CS50
10 CS50 2014 - Week 7
CS50 2014 - Week 7
CS50
11 CS50 2014 - Week 7, continued
CS50 2014 - Week 7, continued
CS50
12 Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
Breaking Through The (Google) Glass Ceiling by Christopher Bartholomew
CS50
13 Introduction to Amazon Web Services by Leo Zhadanovsky
Introduction to Amazon Web Services by Leo Zhadanovsky
CS50
14 CS50 2014 - Week 9
CS50 2014 - Week 9
CS50
15 How to Build Innovative Technologies by Abby Fichtner
How to Build Innovative Technologies by Abby Fichtner
CS50
16 Light Your World (with Hue Bulbs) by Dan Bradley
Light Your World (with Hue Bulbs) by Dan Bradley
CS50
17 Building Dynamic Web Apps with Laravel by Eric Ouyang
Building Dynamic Web Apps with Laravel by Eric Ouyang
CS50
18 CS50 2014 - CS50 Lecture by Steve Ballmer
CS50 2014 - CS50 Lecture by Steve Ballmer
CS50
19 CS50 2014 - Week 10
CS50 2014 - Week 10
CS50
20 This is CS50 with Steve Ballmer?
This is CS50 with Steve Ballmer?
CS50
21 Meteor: a better way to build apps by Roger Zurawicki
Meteor: a better way to build apps by Roger Zurawicki
CS50
22 Data Analysis in R by Dustin Tran
Data Analysis in R by Dustin Tran
CS50
23 Data Visualization and D3 by David Chouinard
Data Visualization and D3 by David Chouinard
CS50
24 CS50 2014 - Week 6
CS50 2014 - Week 6
CS50
25 Build Tomorrow's Library by Jeffrey Licht
Build Tomorrow's Library by Jeffrey Licht
CS50
26 CS50 2014 - Week 9, continued
CS50 2014 - Week 9, continued
CS50
27 Essential Scale-Out Computing by James Cuff
Essential Scale-Out Computing by James Cuff
CS50
28 iOS App Development with Swift by Dan Armendariz
iOS App Development with Swift by Dan Armendariz
CS50
29 Sam Clark Leads Yale Students on Tour to CS50 at Harvard
Sam Clark Leads Yale Students on Tour to CS50 at Harvard
CS50
30 3D Modeling and Manufacture by Ansel Duff
3D Modeling and Manufacture by Ansel Duff
CS50
31 CS50 2014 - Week 5, continued
CS50 2014 - Week 5, continued
CS50
32 hello, world
hello, world
CS50
33 CS50 2014 - Deep Thoughts - Hash Table
CS50 2014 - Deep Thoughts - Hash Table
CS50
34 CS50 2014 - Deep Thoughts - Binary Tree
CS50 2014 - Deep Thoughts - Binary Tree
CS50
35 CS50 2014 - Deep Thoughts - Scratch
CS50 2014 - Deep Thoughts - Scratch
CS50
36 CS50 2014 - Deep Thoughts - MySQL
CS50 2014 - Deep Thoughts - MySQL
CS50
37 LaunchCode Visits CS50
LaunchCode Visits CS50
CS50
38 CS50 Live, Episode 100
CS50 Live, Episode 100
CS50
39 CS50 Field Trip to Google
CS50 Field Trip to Google
CS50
40 This is CS50 AP
This is CS50 AP
CS50
41 Week 4: Monday - CS50 2011 - Harvard University
Week 4: Monday - CS50 2011 - Harvard University
CS50
42 Week 2: Wednesday - CS50 2011 - Harvard University
Week 2: Wednesday - CS50 2011 - Harvard University
CS50
43 Week 1: Wednesday - CS50 2011 - Harvard University
Week 1: Wednesday - CS50 2011 - Harvard University
CS50
44 Week 11: Monday - CS50 2011 - Harvard University
Week 11: Monday - CS50 2011 - Harvard University
CS50
45 Week 3: Wednesday - CS50 2011 - Harvard University
Week 3: Wednesday - CS50 2011 - Harvard University
CS50
46 Week 12: Monday - CS50 2011 - Harvard University
Week 12: Monday - CS50 2011 - Harvard University
CS50
47 Week 1: Friday - CS50 2011 - Harvard University
Week 1: Friday - CS50 2011 - Harvard University
CS50
48 Week 3: Monday - CS50 2011 - Harvard University
Week 3: Monday - CS50 2011 - Harvard University
CS50
49 Week 10: Wednesday - CS50 2011 - Harvard University
Week 10: Wednesday - CS50 2011 - Harvard University
CS50
50 Week 2: Monday - CS50 2011 - Harvard University
Week 2: Monday - CS50 2011 - Harvard University
CS50
51 Week 9: Monday - CS50 2011 - Harvard University
Week 9: Monday - CS50 2011 - Harvard University
CS50
52 Week 7: Monday - CS50 2011 - Harvard University
Week 7: Monday - CS50 2011 - Harvard University
CS50
53 Week 5: Monday - CS50 2011 - Harvard University
Week 5: Monday - CS50 2011 - Harvard University
CS50
54 Week 5: Wednesday - CS50 2011 - Harvard University
Week 5: Wednesday - CS50 2011 - Harvard University
CS50
55 Week 7: Wednesday - CS50 2011 - Harvard University
Week 7: Wednesday - CS50 2011 - Harvard University
CS50
56 Week 8: Monday - CS50 2011 - Harvard University
Week 8: Monday - CS50 2011 - Harvard University
CS50
57 Week 9: Wednesday - CS50 2011 - Harvard University
Week 9: Wednesday - CS50 2011 - Harvard University
CS50
58 Week 8: Wednesday - CS50 2011 - Harvard University
Week 8: Wednesday - CS50 2011 - Harvard University
CS50
59 Week 10: Monday - CS50 2011 - Harvard University
Week 10: Monday - CS50 2011 - Harvard University
CS50
60 Week 2: Wednesday - CS50 2010 - Harvard University
Week 2: Wednesday - CS50 2010 - Harvard University
CS50

Related Reads

📰
Fable 5 Costs 5x More Per Task Than Grok 4.5. Here’s Whether That Actually Matters.
Fable 5 costs 5x more per task than Grok 4.5, but its capabilities may justify the extra cost
Medium · AI
📰
Why Modern Test Automation Frameworks Need AI in 2026
Learn how AI is revolutionizing test automation frameworks in 2026 and why it's crucial for modern engineering teams
Medium · Programming
📰
If AI Writes Your Code, Why Learn Python?
Explore the implications of AI-generated code on programming skills and the continued relevance of learning Python
Medium · AI
📰
Stop Hallucinating Terraform: Building a Private Internal Developer Portal with RAG and Gemma 2
Learn to build a secure Internal Developer Portal using Terraform RAG pipeline and Gemma 2 to stop hallucinating AI code
Medium · AI
Up next
SEO with Claude Code — The Complete Course
Conor Martin
Watch →