Python by Swaroop C H - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

Using Function Parameters

Example 7.2. Using Function Parameters

#!/usr/bin/python
# Filename: func_param.py def printMax(a, b):
if a > b:
print a, 'is maximum' else:
print b, 'is maximum' printMax(3, 4) # directly give literal values x = 5
y = 7
printMax(x, y) # give variables as arguments

Output

$ python func_param.py
4 is maximum
7 is maximum

How It Works

Here, we define a function calledprintMax where we take two parameters calleda andb. We find out the greater number using a simpleif..else statement and then print the bigger number.

In the first usage of printMax, we directly supply the numbers i.e. arguments. In the second usage, we call the function using variables.printMax(x, y) causes value of argumentx to be assigned to parametera and the value of argumenty assigned to parameterb. The printMax function works the same in both the cases.