Python Function
Functions
Function are named blocks of code. designed to do one specific job. Information passed to a function is called an argument, and information received by a function is called a parameter.
#A simple function
def greet_user():
"""Dislay a simple greeting."""
print("Hello!")
#calling function
greet_user()
#Passing an argument
def greet_user(username):
"""Display a personalized greeting."""
print("Hello, " + username + "!")
#calling function
greet_user('jesse')
#Default values for parameters
def make_pizza(topping='bacon'):
"""Make a single-topping pizza."""
print("Have a " + topping + " pizza!")
#calling function
make_pizza()
#calling function
make_pizza('pepperoni')
#Returning a value
def add_numbers(x, y):
"""Add two numbers and return the sum."""
return x + y
sum = add_numbers(3, 5)
print(sum)


No comments:
Post a Comment