Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

It's very simple to define your own functions as well.

Code Block
borderStylesolid
titledeclaring a function
borderStylesolid
def Hello():
    return "Hello, World!"

print Hello()
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
Hello, World!

Now it's ok if you don't understand any of that, I'll go through it step-by-step.

...

Arguments are very handy, as they can allow a function to do different things based on the input.

Code Block
borderStylesolid
titlearguments example
borderStylesolid
def Hello(name as string):
    return "Hello, ${name}!"

print Hello("Monkey")
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
Hello, Monkey!

Here it is again, step-by-step.

...

Function overloading takes place when a function is declared multiple times with different arguments.

Code Block
borderStylesolid
titleoverloading example
borderStylesolid
def Hello():
    return "Hello, World!"

def Hello(name as string):
    return "Hello, ${name}!"

def Hello(num as int):
    return "Hello, Number ${num}!"

def Hello(name as string, other as string):
    return "Hello, ${name} and ${other}!"

print Hello()
print Hello("Monkey")
print Hello(2)
print Hello("Cat", "Dog")
No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
Hello, World!
Hello, Monkey!
Hello, Number 2!
Hello, Cat and Dog!

...

There is a way to pass an arbitrary number of arguments.

Code Block
borderStylesolid
titlevariable arguments example
borderStylesolid
def Test(*args as (object)):
    return args.Length

print Test("hey", "there")
print Test(1, 2, 3, 4, 5)
print Test("test")

a = (5, 8, 1)
print Test(*a)

No Format
bgColor#D8DDE9
titleOutput
borderStylesolid
titleOutput
2
5
1
3

The star * lets it known that everything past that is arbitrary.
It is also used to explode parameters, as in print Test(*a) causes 3 arguments to be passed.
You can have required parameters before the *args, just like in any other function, but not after, as after all the required parameters are supplied the rest are past into your argument array.

...