
Syntax: Function Parameters and Arguments
Creating functions that use input values.
Function Parameters and Arguments
Practice: Function to Greet in Different Languages
-
Define a function called
greetthat takes two parameters:nameandlanguage. -
Inside the function, use an
ifstatement to print different greetings based on the language. If the language is"Spanish", print"Hola, " + name + "!". If the language is"French", print"Bonjour, " + name + "!". If the language is not recognized, default to English and print"Hello, " + name + "!". -
After the function definition, call your
greetfunction with the string"Alice"as the name and"Spanish"as the language. -
Run your code and confirm that the output is
"Hola, Alice!".
HINT*: Click here for a hint
> "Hola, Alice!"
SOLUTION*: Click here for an example solution
def greet(name, language):
if language == "Spanish":
print("Hola, " + name + "!")
elif language == "French":
print("Bonjour, " + name + "!")
else:
print("Hello, " + name + "!")
greet("Alice", "Spanish")
Practice: Function to Calculate the Area of a Rectangle
-
Define a function called
rectangle_areathat takes two parameters:lengthandwidth. -
Inside the function, calculate the area of the rectangle (length multiplied by width) and print the result.
-
After the function definition, call your
rectangle_areafunction with the numbers5and7as the length and width, respectively. -
Run your code and confirm that the output is
35.
HINT*: Click here for a hint
> 35
SOLUTION*: Click here for an example solution
def rectangle_area(length, width):
print(length * width)
rectangle_area(5, 7)
Practice: Function to Convert Celsius to Fahrenheit
-
Define a function called
celsius_to_fahrenheitthat takes one parameter:celsius. -
Inside the function, convert the Celsius temperature to Fahrenheit (multiply by 9, divide by 5, then add 32) and print the result.
-
After the function definition, call your
celsius_to_fahrenheitfunction with the number0as the Celsius temperature. -
Run your code and confirm that the output is 32.
HINT*: Click here for a hint
> 32
SOLUTION*: Click here for an example solution
def celsius_to_fahrenheit(celsius):
print(celsius * 9/5 + 32)
celsius_to_fahrenheit(0)