功能:多个参数

ark*_*ade 1 python arguments function new-operator

我是 python 新手,目前正在当地大学就读初级 python 课程。这是我获得 ICS 学位的要求。昨天我做了一个测验,问题是“以下什么是参数。我知道 def register_for_class(studen_id, crn): 是参数,我知道函数调用是参数,但其他任何东西都被视为参数吗?我的老师不会告诉我。她只是说,不仅仅是函数调用作为参数。

def register_for_class(student_id, crn): # parameter
    add_class(student_id, crn, 2022, 'Spring')
    student_name = get_name(student_id)
    class_name = get_class(crn)
    msg = student_name + ' is now enrolled in: ' + class_name
    return msg

register_for_class(19283746, 22319) # function call 
Run Code Online (Sandbox Code Playgroud)

Bil*_*ard 5

参数是出现在函数定义中的变量名称,因此在您的示例中:

def register_for_class(student_id, crn):
Run Code Online (Sandbox Code Playgroud)

student_idcrn是参数。

参数是调用函数时传递给函数的值,因此当您调用时:

register_for_class(19283746, 22319)
Run Code Online (Sandbox Code Playgroud)

1928374622319是参数。

函数定义中还进​​行了多个函数调用register_for_class,因此如果您的老师说有更多参数,她可能就是指这些。例如:

add_class(student_id, crn, 2022, 'Spring') # four arguments passed here
Run Code Online (Sandbox Code Playgroud)

注 1:变量既可以是形参,也可以是自变量(例如student_id上面的)。

注 2:许多人交替使用术语“参数”“参数” 。我上面给出的定义在技术上是正确的,但你应该知道大多数人在对话中并没有区分两者。