0% found this document useful (0 votes)
7 views6 pages

Python Functions: def and return Explained

Uploaded by

sandboxpmu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Python Functions: def and return Explained

Uploaded by

sandboxpmu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🐍 Python def and return — Complete Guide

1. Defining Functions with def


A function is a reusable block of code. In Python, functions are defined using the def keyword:

def function_name(parameters):
# indented code block

• def — defines the function


• function_name — the name of the function
• parameters — optional input values
• : — ends the function header
• code inside runs when the function is called

Example

def greet():
print('Hello, Python!')

greet()

Output:

Hello, Python!

2. Parameters and Arguments

def say_hello(name):
print('Hello', name)

say_hello('Rako')
say_hello('Amina')

Output:

Hello Rako
Hello Amina

✅ Parameters are placeholders inside the function, arguments are actual values passed during a call.

1
3. Default Parameters

def hero(name='Conan'):
print('Hello', name)

hero() # uses default


hero('Naruto') # overrides default

Output:

Hello Conan
Hello Naruto

4. Multiple Parameters

def mth(num1, num2):


print(num1 + num2)

mth(5, 10)

Output:

15

With defaults:

def mth(num1=5, num2=10):


print(num1 + num2)

mth()

Output:

15

5. User Input with Functions

def welcome(name):
print('Hello ' + name)

2
you = input('Enter your name: ')
welcome(you)

Example Output:

Enter your name: Rako


Hello Rako

6. return Statement — Sending Data Back


print() shows the output, but return gives the result back to the program.

def addnum(a, b):


return a + b

result = addnum(3, 3)
print(result)

Output:

You can use returned values later:

x = addnum(10, 5)
y = addnum(2, 3)
total = x * y
print(total)

Output:

75

7. Returning Multiple Values

def userdata():
name = 'rako'
age = 25
return name, age

3
# Option 1: unpack
ur_name, ur_age = userdata()
print('Name:', ur_name)
print('Age:', ur_age)

# Option 2: tuple
print(userdata())

Output:

Name: rako
Age: 25
('rako', 25)

8. print() vs return

Feature print() return

Shows output on screen ✅ Yes ❌ No

Gives value back to program ❌ No ✅ Yes

Stops the function ❌ No ✅ Yes

Usage For displaying info For calculations, passing data

def add_print(a, b):


print(a + b)

def add_return(a, b):


return a + b

x = add_print(3, 3)
y = add_return(3, 3)

print('x =', x)
print('y =', y)

Output:

6
x = None
y = 6

9. Functions with Conditions

4
def check_age(age):
if age >= 18:
return 'Adult'
else:
return 'Minor'

result = check_age(20)
print(result)

Output:

Adult

10. Best Practices


• ✅ Use meaningful function names.
• 📥 Use parameters to make functions flexible.
• 🧠 Use return when you need the result.
• 💬 Use print only for showing info.
• ✨ Keep functions short and focused.

📌 Full Example

def profile(name, age):


return f'Name: {name}', f'Age: {age}'

user_name = input('Enter your name: ')


user_age = int(input('Enter your age: '))

info = profile(user_name, user_age)


print(info)

n, a = info
print(n)
print(a)

Example Output:

Enter your name: Rako


Enter your age: 25
('Name: Rako', 'Age: 25')
Name: Rako
Age: 25

5
✅ Summary: - def defines a function. - () calls it. - print() shows results. - return gives data
back. - Functions = clean, reusable, powerful code 💪

You might also like