CampusLearn
← CS 101 Introduction to Programming

Functions and scope

Notes

A function packages a set of instructions under a name so you can reuse it. WHY FUNCTIONS MATTER - Reuse: write once, call many times. - Clarity: each function does one job with a clear name. - Testing: small functions are easy to check. PARAMETERS AND RETURN VALUES Parameters are the inputs; the return value is the output. A function that returns nothing is usually doing an action (printing, saving). SCOPE Variables created inside a function only exist inside it. Variables created outside are visible everywhere — avoid relying on that, pass values in as parameters instead. Practice: write a function average(numbers) that returns the mean of a list.

Quiz

1. What is the main benefit of packaging code into functions?

2. A variable created inside a function can be used…

Sign in to submit and securely grade your quiz.