Python- Day 24- FUNCTIONS- Return Values
- A function can process data and return a value or set of values.
- The value returned is called a return value.
- The
return
statement sends the value back to the line where the function was called. - Benefits: Simplifies the main program by delegating work to functions.
def add_numbers(a, b):
return a + b
result = add_numbers(5, 3)
print(result) # Output: 8
Returning a Simple Value:
A function can return a single, formatted result based on input.
For example, Formatting a person’s full name
def get_formatted_name(first_name, last_name):
full_name = f"{first_name} {last_name}"
return full_name.title()
musician = get_formatted_name('Nidhi', 'Ashtikar')
print(musician) # Output: Nidhi Ashtikar
The get_formatted_name()
function combines the first and last name, and formats it using title()
, and returns the result.
Optional Arguments:
Default argument values can be used to make parameters optional.
Allows more flexible function calls where some information may not always be necessary.
def get_formatted_name(first_name, last_name, middle_name=''):
if middle_name:
full_name = f"{first_name} {middle_name} {last_name}"
else:
full_name = f"{first_name} {last_name}"
return full_name.title()
# Calling the function with and without a middle name:
musician1 = get_formatted_name('Nidhi', 'ashtikar')
musician2 = get_formatted_name('nidhi', 'Ashtikar', 'Nid')
print(musician1) # Output: Nidhi Ashtikar
print(musician2) # Output: Nidhi Nid Ashtikar
Middle name is made optional by providing a default empty string middle_name=''
.
The function checks if the middle name is provided, and formats accordingly.
Returning a Dictionary:
Functions can return complex data structures like dictionaries.
This allows organizing related data in a structured format.
def build_person(first_name, last_name):
person = {'first': first_name, 'last': last_name}
return person
musician = build_person('nidhi', 'ashtikar')
print(musician) # Output: {'first': 'nidhi', 'last': 'ashtikar'}
The function stores the first and last names in a dictionary and returns the entire dictionary.
Adding Optional Dictionary Values:
Use None
as a default value for optional arguments that may not always be provided.
def build_person(first_name, last_name, age=None):
person = {'first': first_name, 'last': last_name}
if age:
person['age'] = age
return person
musician = build_person('nidhi', 'ashtikar', age=35)
print(musician) # Output: {'first': 'nidhi', 'last': 'ashtikar', 'age': 35}
The optional age
parameter is added to the dictionary only if it’s provided.
def build_person(first_name, last_name, age=None):
person = {'first': first_name, 'last': last_name}
if age:
person['age'] = age
return person
musician = build_person('nidhi', 'ashtikar')
print(musician) # Output: {'first': 'nidhi', 'last': 'ashtikar'}
Using Functions in a Loop:
Functions can be used within loops to repeatedly perform actions based on user input.
Example: A function to format names used in a while
loop to greet users.
def get_formatted_name(first_name, last_name):
full_name = f"{first_name} {last_name}"
return full_name.title()
while True:
print("\nPlease tell me your name:")
f_name = input("First name: ")
if f_name == 'q':
break
l_name = input("Last name: ")
if l_name == 'q':
break
formatted_name = get_formatted_name(f_name, l_name)
print(f"\nHello, {formatted_name}!")
>>
Please tell me your name:
First name: Nidhi
Last name: Ashtikar
Hello, Nidhi Ashtikar!
Please tell me your name:
First name: q
>
The user can repeatedly input their name, and the program will greet them. The loop continues until the user enters ‘q’ to quit.
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!