Function Parameters Python Tutorial




The idea of function parameters in Python is to allow a programmer who is using that function, define variables dynamically within that function. For example:

def simple_addition(num1,num2):
    answer = num1 + num2
    print('num1 is', num1)
    print(answer)

simple_addition(5,3)

Here, we defined our function name as simple_addition. In the function parameters (often called parms for short), we specify variables named num1 and num2.

Next, within the function, we say this new answer variable is equal to whatever num1 plus num2 is. We then print out what num1 is, whatever it happens to be. Finally, the last line of this function just prints out the answer variable, which is num1 plus num2.

Now, to run this function and make use of these parameters, we run simple_addition(5,3). This runs the simple_addition function using the parameters of num1=5 and num2=3. Then our program sums 5 and 3 together, then we print out that num1 is 5, and finally we print out the "answer" which was defined already, which is the sum of 5 and 3, which is of course 8.

There is no limit to the amount of function parameters you have. If you want to just specify the definitions of these parameters without saying the parameter, like when we just said 5 and 3, instead of putting the parameter=5, then you must put them in the exact order. If you have a lot of parameters where it might be difficult to remember their order, you could do something like:

simple_addition(num2=3,num1=5)

In that case, when you call the function and define the parameters, you can see how we actually defined num2 before num1, even though in the function definition we ask for them in the other way around. As long as you specify the parameter you are defining, you can jumble them up. Otherwise, you must keep them in order!

Finally, not only must they be in perfect order, but you must not specify too many or two few definitions.

This will not work:

simple_addition(3,5,6)
nor will this:
simple_addition(3)

There exists 1 challenge(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:





  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial