Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're going to talk about functions in Python. Can anyone tell me what a function is?
Isn't it a block of code that does something?
Exactly, Student_1! Functions allow us to group code together, which we can call multiple times without rewriting it.
So, they help us avoid repeating ourselves, right?
Yes, that's called DRY - Don't Repeat Yourself. Functions are a way to keep our code clean and easy to understand.
How do we create a function?
Great question! We define a function with the def keyword, followed by the function name and parentheses. Let’s look at an example. Can anyone tell me how we might write a function to greet someone?
It would be def greet(): and then we'd say something like print("Hello!")).
Exactly, Student_4! Nicely done! Let’s keep this structure in mind as we move on.
Now, let's discuss how functions can accept inputs, called parameters. Can anyone share what a parameter might be?
Is it like a variable that you give to the function to use?
Exactly, Student_1! Parameters allow functions to operate on different data. For example, let's say we have a function that adds two numbers - def add(a, b): return a + b. What do you think a and b are?
They're the inputs to the function!
That's right! And when we call this function, we can input any numbers we want. Let's see this in action.
We can call it like add(3, 5)?
Yes! And that will return 8. Now, which one of you can write out that function and call it for us?
Now that we have parameters and our function structure down, let's understand return values. Why are return values important?
They let us use the output of a function elsewhere in our code.
That's correct! When we use return in our function, it sends back a value. For example: def multiply(x, y): return x * y. What will multiply(2, 3) give us?
That would be 6!
Exactly, well done! So when you want to utilize the result of a function, you must store it in a variable. Let's practice this in our next exercise!
Functions return like a boomerang, passing back value with a little clang.
Once there was a magician (function) who had a magic box (parameter). Whatever you put in the box, it would return something special to you (return value).
To remember how to create a function: 'D PR' - Define, Parameters, Return.
F.A.R. - Functions, Accept inputs, Return values.
Term
What is a function?
Click to flip
Term
What keyword defines a function?
Click to flip
Term
What is a return value?
Click to flip
Functions in Python allow for code reuse and better organization.
In Python, functions are blocks of reusable code that can be defined to perform specific tasks. They can take parameters and return values, which enhances modularity and organization in coding practices.
Functions are essential components in Python, allowing for code reuse and better organization. A function is defined using the def keyword, followed by a function name and parentheses containing any parameters. By creating functions, developers can encapsulate tasks, making the code more modular and easier to maintain.
def greet():
print("Hello, AI Student")
greet()
# Function with Parameters:
def add(a, b):
return a + b
result = add(3, 5)
print(result) # Outputs 8Functions are foundational in structuring Python projects, especially in larger applications, and understanding them is critical as one progresses in programming.