Introduction
Functions are a fundamental building block in Python programming, allowing you to encapsulate code into reusable blocks. A function can take inputs, perform a specific task, and optionally return a value. This tutorial will guide you through creating a simple function in Python.
Example:
- Function Name:
greet_user - Function Purpose: Print a greeting message.
- Program Output:
Hello, Ramesh!
Problem Statement
Create a Python program that:
- Defines a simple function named
greet_user. - The function takes a single argument, the user’s name.
- The function prints a greeting message using the provided name.
- Calls the function with a sample name to demonstrate its usage.
Solution Steps
- Define the Function: Use the
defkeyword to define a function namedgreet_user. - Add Parameters: Add a parameter to the function to accept the user’s name.
- Add Function Logic: Inside the function, print a greeting message that includes the user’s name.
- Call the Function: Call the function with a sample name to demonstrate its functionality.
Python Program
# Python Program to Create a Simple Function
# Author: https://www.rameshfadatare.com/
# Step 1: Define the function
def greet_user(name):
# Step 3: Add function logic
print(f"Hello, {name}!")
# Step 4: Call the function with a sample name
greet_user("Ramesh")
Explanation
Step 1: Define the Function
- The function is defined using the
defkeyword, followed by the function namegreet_user. - The function accepts one parameter,
name, which is used to pass the user’s name to the function.
Step 2: Add Parameters
- The parameter
nameallows the function to accept an argument when it is called. This argument will be the name of the user that the function will greet.
Step 3: Add Function Logic
- Inside the function, the
print()function is used to display a greeting message. Thef-stringsyntax (f"...") is used to format the string, inserting the value ofnameinto the message.
Step 4: Call the Function
- The function
greet_user("Ramesh")is called with the argument"Ramesh". This passes the name "Ramesh" to the function, which then prints the greeting message.
Output Example
Example Output:
Hello, Ramesh!
Additional Examples
Example 1: Function with Multiple Parameters
# Function to greet a user with their first and last name
def greet_user(first_name, last_name):
print(f"Hello, {first_name} {last_name}!")
# Calling the function with first and last name
greet_user("Ramesh", "Fadatare")
Output:
Hello, Ramesh Fadatare!
Example 2: Function with a Default Parameter
# Function with a default parameter for the name
def greet_user(name="Guest"):
print(f"Hello, {name}!")
# Calling the function without an argument uses the default value
greet_user()
# Calling the function with an argument overrides the default value
greet_user("Ramesh")
Output:
Hello, Guest!
Hello, Ramesh!
Example 3: Function that Returns a Value
# Function that returns a greeting message
def create_greeting(name):
return f"Hello, {name}!"
# Calling the function and storing the return value
greeting_message = create_greeting("Ramesh")
# Printing the returned greeting message
print(greeting_message)
Output:
Hello, Ramesh!
Conclusion
This Python program demonstrates how to create a simple function using the def keyword. Functions allow you to encapsulate code into reusable blocks, making your programs more modular and easier to maintain. By defining parameters, you can make functions more flexible, allowing them to accept input and perform operations based on that input. Understanding how to create and use functions is essential for writing efficient and organized Python code.