Learn Python: Defining and Calling Functions
Functions in Python let you group statements under a name and call them whenever you need that logic. They help keep your code organized, reduce repetition, and make it easier to test individual components. In this lesson, you will define simple functions that accept parameters and possibly return results. You’ll also see the difference between returning a value and just printing one. Working in Visual Studio Code (VS Code), you can store these functions in a file and run them to confirm each behaves as intended.
What will be covered
• Writing your first function using def.
• Understanding function parameters and how to pass arguments.
• Distinguishing between printing and returning a value.
• Local variables within functions vs. global scope (brief overview).
• Examples of functions like greet(name) and square(x).
Defining a Basic Function
Use the def keyword, followed by the function name, parentheses for parameters, and a colon to introduce the function body. Indent the function body in Python. A return statement sends a value back to the caller.
def greet(name):
return "Hello, " + name
message = greet("Alice")
print(message)
The greet function creates a string with the user’s name and returns it to the caller, which then prints it. You could also print directly in the function, but returning values provides more flexibility.
Parameters and Arguments
A function defines parameters in its signature. When you call the function, you provide arguments that match those parameters. This allows the function to operate on different input without duplicating code.
def add_three_numbers(a, b, c):
return a + b + c
result1 = add_three_numbers(1, 2, 3)
result2 = add_three_numbers(10, 20, 30)
print(result1, result2)
In this example, a, b, and c are parameters. We call the function twice with different arguments, and it computes the sum each time, returning it to be printed.
Printing vs Returning
If a function prints a value, that output goes directly to the console. If it returns a value, you can store that result in a variable, manipulate it, or pass it to another function. Returning is often more flexible, since you decide how to use the value. Printing is best for quick status messages or debug output you don’t need to keep.
Local Variables
Variables inside a function are local to that function. Once the function ends, those local variables vanish. If you define a variable x in the main body, it is separate from an x inside a function. This concept is called scope, and it helps prevent naming collisions. The next lesson will discuss scope in detail.
Practice Exercises
- Write a function add_three_numbers(a, b, c) that returns the sum of its three parameters. Test it with different numbers and print each result.
- Create a function is_even(number) that returns True if the number is even, or False if it’s odd. Call it in a loop for numbers 1 through 5, printing whether each number is even or odd.
- Define a function greet_user(name) that prints a greeting like "Hello, name!" without returning anything. Call this function for at least two names. Then modify it to return the greeting string instead, and print the returned result.
- (Stretch) Write a function calculate_grade(score) that converts a numeric score (0-100) to a letter grade: A if 90 or above, B if 80-89, C if 70-79, D if 60-69, otherwise F. Call this function with different scores and print the results.
Summary
Functions let you group logic and reuse it whenever you need. By defining parameters, you can adapt the function’s behavior to different inputs. Whether you choose to print inside the function or return a result depends on your needs. Returning is typically more flexible because it allows callers to further process the data. Local variables remain confined to the function, avoiding accidental interference with other parts of the program. In the next lessons, you’ll see how functions interact with scopes and more advanced function features like default arguments or variable-length parameters.