Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 6 - Functions - Section 6.5 - Positional and Keyword Arguments - Check Point - MyProgrammingLab - Page 180: 6.11

Answer

A function’s arguments can be passed as positional arguments or keyword arguments.

Work Step by Step

There are two kinds of arguments: positional arguments and keyword arguments. Using positional arguments requires that the arguments be passed in the same order as their respective parameters in the function header. For example, the following function prints a message n times: def nPrintln(message, n): $ $ $ $ $ $ $ $ $ $ $ $ for i in range(n): $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ print(message) You can use nPrintln('a', 3) to print a three times. The nPrintln('a', 3) statement passes a to message, passes 3 to n, and prints a three times. However, the statement nPrintln(3, 'a') has a different meaning. It passes 3 to message and a to n. When we call a function like this, it is said to use positional arguments. The arguments must match the parameters in order, number, and compatible type, as defined in the function header. You can also call a function using keyword arguments, passing each argument in the form name value. For example, nPrintln(n = 5, message = "good") passes 5 to n and "good" to the message. The arguments can appear in any order using keyword arguments. It is possible to mix positional arguments with keyword arguments, but the positional arguments cannot appear after any keyword arguments. Suppose a function header is: def f(p1, p2, p3): You can invoke it by using f(30, p2 = 4, p3 = 10) However, it would be wrong to invoke it by using f(30, p2 = 4, 10) because the positional argument 10 appears after the keyword argument p2 = 4.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.