Search code snippets, questions, articles...

Create a function in python

def function_name():
    print("Hello this is a function")

#To call the above function use
function_name()
Output
Hello this is a function

You can create a function in python by using the 'def' keyword before the function name. The above code snippet function will print - Hello this is a function when called. You can also use the function to return some values or data when called.

Was this helpful?
1 Comments

You can also pass parameters to a function and use them inside the function code block.

Passing parameters to function

def hello(username):
    print("Hello," + username)


hello("John")

The code will output 'Hello John' as we called the function by passing 'John' as 'username' parameter value.

Programming Feeds
Learn something new everyday on Devsheet