Scripting >> Python >> Examples >> Functions >> defining functions

 

eg. 1 - Function with no arguments and no return value

def say_hello():
    print("Hello")

# call it by
say_hello()

eg. 2 - Function with argument(s) and no return value

def say_hello(yourname):
    print("Hello {}".format(yourname))

# call it by
say_hello("James")

e.g. 3 - Function with argument(s) having a default value and no return value def say_hello(yourname="John Doe"):
    print("Hello {}".format(yourname))

# call with value
say_hello("Mary")
# call with default value
say_hello()
e.g. 4 Function with argument(s) and return value def timesfive(x):
    return 5*x

# call and use the return value
x = 2
answer = timesfive(x)