Python functions with multiple arguments

Passing multiple arguments to the functions

We have seen how to write a function and how to pass an argument to it. Advanced level programming often requires functions that can take multiple arguments. The format for multiple arguments is as follows:

def your_function(arg1,arg2,arg3):
statement 1#
statement 2#
……..

Look at the example given below:

def print_info(arg1, arg2):
    print(arg1 + " " + arg2)

arg1 = input("what is the first argument?")
arg2 = input("what is the second argument?")
print_info(arg1, arg2)

Download the code Run the code

When you run the code you will be prompted to enter first argument. Type the word “Hello”. For the second word type “World”. The output will be “Hello World”

Now, have a look at the next example:

def add_info(arg1, arg2, arg3):
    sum = int(arg1)+int(arg2)+int(arg3)
    print(str(sum))

num1 = input("what is the first number?")
num2 = input("what is the second number?")
num3 = input("what is the third number?")
add_info(num1, num2, num3)

Download the code Run the code

As you can see the above example is about addition of three numbers. When you feed in values of numbers, they are taken in as string values. So, in add_info() function the values are first converted to integer. Then to print the values, the sum of the numbers is again converted to a string.

<<< Comprehension Lists in PythonException Handling in Python >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .