In this video, we generalize the syntax and semantics of
the function call expression to support
multiple arguments instead of zero or one argument.
Here is the current syntax for a function call,
with zero or one argument that you have already seen.
Different functions actually support different numbers of arguments.
For example, the len function has exactly one argument.
The function randint from the Random module in the standard library has two arguments.
This function returns a random number between its two argument objects.
Many Python functions can accept a variable number of arguments.
For example, when the print function is called with no arguments,
it displays a blank line.
When the print function is called with more than one argument,
it evaluates its argument expressions and displays
all the results objects on one line separated by single blanks.
Here's the documentation for the built-in print function.
The symbol star before the first argument
"objects" indicates that this function accepts any number of arguments,
zero or more at this argument position.
That is why I could successfully co-print with one argument,
no arguments, or three arguments.
We will ignore the rest of
the print function argument documentation until a future lesson.
I will generalize the function call syntax to support more than one argument.
Here is a simplified syntax diagram for
the non-terminal argument list state that supports one or more argument expressions.
I will use these syntax diagrams to show that print of 27,
hello, and four, has valid syntax.
There are eight tokens in this line.
From the start state,
the identifier token print matches the identifier state.
The left parenth delimiter token matches the left parenth state.
The next state is the non-terminal state argument list.
Expand this non terminal state to reveal the expression state.
Expand the expression state to reveal its literal,
identifier, function call, and binary expression states.
The literal integer token 27 matches the literal accepting state,
so the expression is valid.
The next token is the comma delimiter which matches the comma state.
The next token is the literal string token hello,
expanding the expression state again reveals
its internal states and hello matches the literal state,
so this expression is valid.
The next token is the comma delimiter which matches the comma state.
The next token is the integer literal four.
Expanding the expression state again finds a match with the literal state,
so the expression is valid.
The next token is the right parenth delimiter token,
so the interpreter does not want to match anymore expressions.
Since the current expression state is
an accepting state in the argument list non-terminal state,
argument list is valid.
Returning to the syntax diagram for function call,
the right parenth token matches the right parenth state.
There are no more tokens.
Since the current state is an accepting state for the function call,
the function call is valid.
Since function call is an accepting state in the expression syntax diagram,
the entire expression has valid syntax.
Since the syntax is valid,
the interpreter can apply semantic analysis.
Here is the previous semantic rule for evaluating
a function call with zero or one arguments.
I will modify this semantic role to support multiple arguments.
Step one is the same.
Step two becomes, if there is
an argument list evaluate it to obtain an argument object list.
Otherwise create an empty argument object list.
Step three becomes, pass the argument object list to the function.
Step four is the same.
The semantics of the non-terminal argument list state R,
evaluate each expression from left to right and add
the result objects to an argument object list in order.
Let's apply these semantic rules to the multi-argument print function call.
When the program starts,
the name print is pre-round to the print function object.
Step one uses the semantics of identifier to
dereference print in the program's namespace to obtain the print function object.
Step two checks if there is an argument list.
Since there is an argument list between the parentheses,
the interpreter applies the semantics of an argument list.
Evaluate each of the three argument expressions using the semantics of literals.
Then, add each result object to the argument object list.
It will contain three objects: integer 27,
string hello, and integer four.
Step three passes this list of three objects to the print function object.
Recall that a function is like a box with
an argument object as input and a result object as output.
The input is actually a list that contains zero or more argument objects.
Step four evaluates the print function object to display the three argument objects: 27,
hello, and four in human readable form each separated by a blank.
The print function returns an object whose type is NoneType,
and this object is ignored.
In this video, we generalized the syntax and semantics of
function calls to support multiple arguments.