Functions Arguments & Parameters
Functions are used for code tasks that are intended to be reused
Python allows us to create User Defined Functions and provides many Built-in Functions such as print()
print()
can be called using arguments (or without) and sends text to standard output, such as the console.print()
uses Parameters to define the variable Arguments that can be passed to the Function.print()
defines multiple string/numbers parameters which means we can send a long list of Arguments toprint()
, separated by commas.print()
can also be called directly with just its name and empty parentheses and it will return a blank line to standard output
(2)
def some_function():
use the def
statement when creating a function
- use a function name that starts with a letter or underscore (usually a lower-case letter)
- function names can contain letters, numbers or underscores
- parenthesis () follow the function name
- a colon : follows the parenthesis
- the code for the function is indented under the function definition (use 4 spaces for this course)
def some_function():
#code the function tasks indented here
The end of the function is denoted by returning to no indentation
(3)Call a function by name
Call a simple function using the function name followed by parenthesis. For instance, calling print isprint()
(4) Functions that have Parameters
print()
and type()
are examples of built-in functions that have Parameters defined
type()
has a parameter for a Python Object and sends back the type of the object
an Argument is a value given for a parameter when calling a function
type
is called providing an Argument - in this case the string "Hello"type("Hello")
Defining Function Parameters
- Parameters are defined inside of the parenthesis as part of a function
def
statement - Parameters are typically copies of objects that are available for use in function code
def say_this(phrase): print(phrase)
Function can have default Arguments
- Default Arguments are used if no argument is supplied
- Default arguments are assigned when creating the parameter list
def say_this(phrase = "Hi"): print(phrase)
翻译:argument:论点
intialize:初始化
define a variable for called phrase:为被调用的短语定义一个变量