Functions - one parameter
Parameter
A parameter is like a placeholder. When a function is called, you pass a value to the parameter.
1 def function_name(parameter): | The keyword def indicates a function is defined. function_name must follow variable naming rules. Parameter is passed in parenthesis. |
2 <indented block> | Indented code to perform some action. |
Example 1
Write a function that squares a number. In the menu option, ask the user for a number. Call the function square with the user’s number as the value. Create a function named square. Print the square of the number.
Example 2
Write a function that determines if a number is even. In the menu option, ask the user for a number. Call the function even with the user’s number as the value. Create a function named even. If the number is even, print the number is even.
Example 3
Write a function that calculates the summation of a number. In the menu option, ask the user for a number. Call the function summation with the user’s number as the value. Create a function named summation.
Last updated