Python (Scripting) - Pass values into a function

If you are not familiar with functions, refer to Getting Started with functions in Python.

AVOID TROUBLE

A function must be define before the function is called in the script. For example, let's say you call the "foo" function before the foo function is defined.

#!/usr/bin/python

Hello "World"

def Hello(greeting):
  print greeting

 

This will return an error that looks something like this.

Traceback (most recent call last):
  File "/home/john.doe/testing.py", line 3, in <module>
    Hello("World")
NameError: name 'Hello' is not defined

 

In this example, values "Hello" and then "World" are passed into the Hello function.

The values passed into the Hello function are placed in the "greeting" variable.

#!/usr/bin/python

def Hello(greeting):
  print greeting

Hello("Hello")
Hello("World")

 

Here is how you would pass two (or more) values into a function.

#!/usr/bin/python

def Hello(one,two):
  print one
  print two

Hello("Hello", "World")

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





Please enter e03c1 in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |