Lecture

Function Arguments and Return Values

Functions often require inputs to perform tasks. These inputs are called arguments.

You can also make functions return values, allowing you to reuse results elsewhere in your code.


1. Positional Arguments

These are the most common type of arguments. You pass values in the exact order the function expects them.

Positional Arguments
def multiply(x, y): print("Product:", x * y) multiply(2, 4)
  • x gets 2, y gets 4
  • The result printed is Product: 8

Instead of just printing, functions can send a value back using return.

Return Values
def multiply(x, y): return x * y result = multiply(2, 4) print("Result:", result)
  • The function calculates the product and returns it.
  • We store the result in a variable and print it later.

2. Keyword Arguments

You can also pass values by name, offering greater flexibility and readability.

Keyword Arguments
def introduce(name, age): print(name, "is", age, "years old.") introduce(age=12, name="Liam")
  • The order of arguments doesn’t matter when using names.
  • This improves clarity and helps prevent mistakes.

3. Default Values

You can assign default values in a function definition to make certain arguments optional.

Default Values
def greet(name="friend"): print("Hello,", name) greet() greet("Sophie")
  • The first call uses the default ("friend").
  • The second call overrides it with "Sophie".

Summary

ConceptDescription
Positional argumentsPassed in order; e.g., func(2, 3)
Keyword argumentsPassed by name; e.g., func(x=2, y=3)
Default argumentsOptional values in the function definition
returnSends a value back to the caller
Quiz
0 / 1

Understanding Function Arguments

When using arguments, the values are passed in the exact order the function expects them.
positional
keyword
default
anonymous

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result