back to course main page

You can create and define your own functions. These are useful when you have some code fragments that need to be reused in multiple locations in your code. You can define you required functionality in a function. This function can have parameters.

Here are a few simple examples.

Hello World

def HelloWorld():
   print("HelloWorld")

# to call this function
HelloWorld()

Now expand this to accept a parameter. In this example it takes a names

def HelloWorld(name):
print(“HelloWorld : Welcome : “, name)

# to call this function
HelloWorld(“Brendan”)

A function can do anything you want.

def add_numbers(num1, num2):
   result = num1 + num2
   return result

# to call this function
add_numbers(1, 2)

back to course main page