Teaching my 40 year old student with no experience programming | Functions

Phil Choi teaches functions to a 40-year-old student learning to code from scratch. They work through how functions let you write code once and reuse it, breaking down the concept with concrete examples so it clicks for someone brand new to programming.
Chapters
And like, I don't know, like, but like, now I think my muscle memory a little bit is coming back. But I'm like, I'm sorry, I'm going on a little rant on this, but it's okay to feel uncomfortable, you know, it means you're getting better, you know, and especially, like, don't ever think I'm going to lead you in, like, the wrong way or something. I'm always, like, trying to say things that make sense in my mind. But it might not make sense for you yet. If it doesn't make sense, you can find other examples and maybe I can try to iterate or verbalize it a different way.
Yeah, yeah, because I remember when you, you had mentioned too just about the variables being like, that pocket, like, if we need, it's going to hold something. And so a lot of times when I was trying to get the return thing, it was because, like, I, I couldn't get it to return, like, where I thought it was going to be. But as long as I made that pocket that you mentioned and like, hey, give this information back to that pocket, like, put it in that pocket, then it would kind of work. Feels like sometimes there's code, if that makes sense.
Yeah, it does make sense. Sometimes you just see a return with no value returned, and that, yeah, yeah, function is returning undefined, but it's running something in the function that's changing. Some outside factor probably. So there's some stuff like that. But if it's always returning a value, you're always going to have to catch it with some variable or something. Okay, so the syntax would be a function space. So kind of like var const and let function a new way to instantiate some new piece of variable. So function, function, name, and then you see the parentheses. And this parameter doesn't always have to be there, but you can pass however many parentheses, however many parameters. You want. This column scope of code in the curly brace. Cool. So we have functions in the function file. Now, functions. Functions are a way to group code together to perform a specific task. So function, function, name and then block of code to be executed. So on line 12, I have, I have instructions. On line 11, let's write a function called greet that takes a name as an argument and returns a greeting message. So let's use the return keyword here.
So then I'm doing.
We can't believe you're not growing. So it's just like, you know, when you're boxing at the gym, you know, and it's too easy or something. You're not really making any piece of your muscle. Getting better. You know, like today I was boxing and I was like, I haven't bought, like, I started boxing and I've been boxing like heavily for like the last month. Like now, like, my, My, like, body and stuff is coming back a little bit, but, like, I forgot to bend my knees like all the way. So I was like. And I was shadow boxing and I was like. And like, I was bending my knees a little bit and I realized, oh, dude, this, this hurts. There's a reason why I wasn't bending my legs, you know?
Yeah.
So now I'm bending my legs and I'm having a lot more balance, you know. But like, I was thinking like, oh, dude, right. Right after I throw a right hand, I don't know why I'm not stepping back anymore.
I can still use name as a variable even though it's in my parameter. So I still could use name.
You could, yeah.
And that's an empty string that I'm using. And then it'll be. Then it would just be return. But that, that doesn't seem exactly.
You don't need the parentheses, though.
So in my return, I can still do like some type of like a formula still, like in a return, no matter what.
Yeah, you can.
Okay. I, I didn't even know. I thought the return was like, hey, it's like it has to be set.
Oh, no, no, no, no. You can do a whole bunch of stuff as long as it's not a promise. But I'll let you know about that in a second. And return promises though, but you have to resolve them as places, so you could. Actually, I'm just going to write some code on line 11, 12, and 13. Like function string. Right. I can just say return string, and I can say function num return 5 and I can say function full return. True. So these guys all have three return statements, right? And then I. If. So basically this will be something like string, right? So if I just ran this, we wouldn't.
We.
We wouldn't be able to do anything with the return value, right?
Well, it's not going to return just the word string.
Yeah, it'll. It'll return string, but we'd have to string call like this. And then now this return value, this string right here will be inside of string call. Okay. So If I just console.log string call, it'll say string. Right. But yeah, I think this might be a little bit confusing here. If I just call string, I wouldn't catch it with anything Right. So it wouldn't. It wouldn't have any output. But if I just console dot log, the return, this would be string. Right. Because this console log is taking the. Is expecting some kind of value inside of here.
Right.
So this could be confusing.
So I guess I don't. I think, like, maybe it's too. Too much is blank right now. Like, because, I mean, whether you're returning or console log or either one of those, it's still going to say string, right? In the terminal.
Exactly.
But if I type in, if there's something in the actual parameter of it. So even if I put like Steve.
But you wouldn't pass Steve here, right? You'd pass name here. Right, Right.
So then if I call it and.
You return name here, that makes sense.
Versus the blank parameter and then returning string, I guess.
Okay. No, I totally understand. So then if I put Steve here. Right, right.
And then it should return. I should see Steve in the comp.
Yeah, exactly. Yeah.
Yeah, that. That makes sense.
Like, I make a lot of these. These functions that return values in my code for some reason, because it's a good way for me to kind of organize things. I'll do something like function. Let's just say code lesson, right? And here I'll just. I'll return something like an ID date lesson name. Then I'll return something like this dot ID equals id. This dot date equals date. This dot lesson. These are called, like factory functions. They. It's not part of the lesson at all. It's just.
Factory functions.
Factory functions are like very clean prototypes that won't mess with your database or anything like that. So today's we can say, like today lesson equals new code lesson. And then we'll call this like. We'll say ID1, and we'll say new dates and 2 ISO string and we'll say lesson name with Shay. And this is kind of like a way that I write my code to keep it very clean and concise. Hans Names. All right, so we got Mark, Tom and Travis. All right, so then we have. And then we have a function called greet. And then that takes a name as a parameter. And just so what we're expecting it to do, right, let's do. It's going to be a string and the name, name person returns. Hello. Hello. Hello, Nate. So that's how it's supposed to look, right? All right, so that's what it's doing now. Let's just do it this way. So then if we do the four, let I equals 0i on names, dot length I plus plus and then we do a console log on greets at names. I. All right, so we should be seeing hello, Mark, hello, Tom, hello, Travis. Right.
Right.
So hello, Mark, hello, Tom, hello, Travis. So then what if I were to say function capitalize name. So this is going to take something called name, right? We'll just call it N. Let's just return n to uppercase plus n dot slice at 1, and then it will capitalize the. So you can kind of encapsulate some logic within there. Right. And the way this capitalized name is through this parameter. Right. And the way we discrete and the crete was able to use something outside was through a parameter. Right. And then I return something to greet and we're able to use that value inside of greet. So let's just say that name is. So that would be the same thing. Is there any questions about this code that we wrote now?
I mean, I, I, I get it, like, what's going on? But at the same time, it's not like, I don't see how is it capitalized name. But, and then, so it's actually the first time that that function capitalized name is actually running is when it's in the return.
So the first time capitalized name is running is on, On Mark.
No, I mean the actual. Because like, you know how we, we create the function. But then when you actually like want to run it, you, you don't type out function all the time. You would just write greet name and then the name. Right, Call it to call the function.
Yeah, yeah, yeah.
So the first time that function capitalized name is actually being called is on line number 28. Is that correct?
Online 26.
Ah. Okay. Okay, okay. Yeah, I don't, I don't know. That looks weird that it's actually like running inside of a variable that's a constant, but it's not.
So we can make this a let. It'll still work the same, but it never gets reassigned. So I just made it.
Okay. Yeah, I guess I didn't know that that was like a thing to run a function in inside of a variable. Or you could call, you could call a function inside of a variable.
Yeah, it's kind of just returning outside of the. It returns the value from that scope to be used outside of that scope, and that's what the return's actually doing.
Holy smokes. Okay, that makes sense. Now, see, like the. Not like, now you're running like that function three different times inside that variable sum. And so then when I console log sums, I should be getting 3, 7, and 11 in a new array.
Correct.
Maybe.
Okay, perfect. So. So then if.
I don't know why that makes more.
Sense to me than the analogy that we had earlier. But it's like putting. Putting things into a bucket, you know?
Yeah, yeah.
Thumbs that length and then I plus plus and then. And I think it's a good use case. Like a good scenario for when you'd use it like you want to. Because you don't want to always be like calculating on the fly. Really. You want to. You want to do it in the beginning and then when like the guy's ready to or a person's ready to like see whatever calculations already happened, you don't want any loading time or waste memory there. Right front load those tasks. So. So now we. If we ran this, it would say, oh, crap. It's oh, crap.
Sorry.
I never. My condition. Do you know why that happened?
I didn't. I didn't see the error. Oh, undefined. Because we never defined what sums was. No, wait, sum, sum, sums, length I +. Oh, we never actually ran. We never called the function.
No, Right.
No, hold on. No, because sums calls it.
Yeah, so just look on line 45. Can you find the error 4 let.
I equals 0 sums.
Oh.
Your eye needs to be less than.
Right. So can you do that on line 45 real quick?
Yep.
Cool. I'll just save it and then I'll run the code. Cool. So we got 3, 7, and 11, right?
More from Phil

day in the life of a software engineer | 5 hour productivity | building my dev agency 🔥

day in the life of a software engineer | 3 hour productivity | coding my 6-figure community startup

day in the life of a software engineer | 5 hour productivity | coding our sns startup app LFG

day in the life of a software engineer | 5 hour productivity | creating apps that makes big money






