Save this video free

Lecture 14: Functions

Burton Ma · 30:14 · Watch on YouTube

Lecture 14: Functions Watch on YouTube →

Overview

Burton Ma explains that Bash functions behave like commands: `return` sets an exit status from 0 to 255, while larger numbers and strings are communicated through standard output and command substitution. He demonstrates dynamic scoping, its risks for shared variables and loops, and how `local` limits variable leakage while remaining visible to called functions.

Key takeaways

Chapters

0:00 Bash Function Return Statuses and the `max` Script
10:00 Command Substitution Overhead and Dynamic Scoping
12:00 Tracing `A` and `B` Through Nested Function Calls
18:00 Using Dynamic Scope to Update the Maximum
21:00 Preventing Variable Collisions with `local`
25:00 Sharing Script Variables and Following Assignment Requirements

Keep these chapters and the full searchable transcript in your own library.

Transcript excerpt

0:02 All right, good afternoon, everyone. Um so, we were talking about bash functions uh in the previous lecture. Um just to quickly remind you, bash functions are a bit are unusual compared to the programming languages you've worked in um already. Uh so, the best way to treat a bash function is as it were just a plain old bash command. Right? And so, in other words, bash commands, they can output stuff, so they can print stuff to standard output. Um and if they don't print something Well, sorry, even if they print something, when they finish, they end up setting an exit status, which is just an integer value. Non-negative integer value. Um and so, that's true of bash commands as well. Uh sorry, bash functions as well. They don't return things like you think

0:50 of a Python function or a Java method returning something. Okay, but there is a section in the notebook called returning a value. So, what the heck's all What What is that all about? So, um you can use the return keyword in bash to return an integer value. The integer value um will always be between 0 and 255, regardless of what value you try to return from the function. So, in other words, bash does something weird with the number that you do uh that you um return, and it gets uh it's always between 0 and 255. So, this is fine if you're writing a function that needs to return a integer value between 0 and 255. Uh but if you need to return an integer value outside of that range, you can't just return the value. You have to do something else. Similarly, if

1:37 you want to return a string, you can't return a string. You have to do

This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.

Related notes

Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Burton Ma.

Want the full transcript?

Save this video in YouTube Collector to get its complete searchable transcript, your own AI summaries, and a library that keeps every video you collect in one place.