Python Positional Arguments: Orderly Function Parameter Handling

thumb_up 1  ·  sell Python positional arguments, Managing function parameters by position in Python, Using positional arguments in Python functions

The list of variables declared in the parentheses at the time of defining a function are the formal arguments. A function may be defined with any number of formal arguments.

While calling a function −

  • All the arguments are required

  • The number of actual arguments must be equal to the number of formal arguments.

  • Formal arguments are positional. They Pick up values in the order of definition.

  • The type of arguments must match.

  • Names of formal and actual arguments need not be same.

Example

 
def add(x,y): z=x+y print ("x={} y={} x+y={}".format(x,y,z)) a=10 b=20 add(a,b)

It will produce the following output −

x=10 y=20 x+y=30

Here, the add() function has two formal arguments, both are numeric. When integers 10 and 20 passed to it. The variable a takes 10 and b takes 20, in the order of declaration. The add() function displays the addition.

Python also raises error when the number of arguments don't match. Give only one argument and check the result.

add(b) TypeError: add() missing 1 required positional argument: 'y'

Pass more than number of formal arguments and check the result −

add(10, 20, 30)
TypeError: add() takes 2 positional arguments but 3 were given

Data type of corresponding actual and formal arguments must match. Change a to a string value and see the result.

a="Hello" b=20 add(a,b)

It will produce the following output −

z=x+y
     ~^~
TypeError: can only concatenate str (not "int") to str



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back