Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Function:

A function is a block of code which only runs when it is


called.
In simple terms, when you want to do something
repeatedly, you can define that something as a function
and call that function whenever you need to.

Syntax of Function:
def function_name(parameters):

# What the function does goes here


return result

 You need to use the def keyword, give your function a name,
followed by a pair of parentheses, and end the line with a
colon (:).
 If your function takes arguments, the names of the arguments
(parameters) are mentioned inside the opening and closing
parentheses.
 When you call the function with specific values for these
parameters, they're called arguments or actual parameters.
This is because the arguments in the function call are the
values used for the function's parameters.

How to Create a Simple Function in Python:

def my_func():

print("Hello! Hope you're doing well")


now call the function my_func() and check the output.

my_func()

# Output
Hello! Hope you're doing well

Create a Function with Arguments:

def my_func(name,place):

print(f"Hello {name}! Are you from


{place}?")

now call the function my_func() and check the output.

my_func("Riya","India")

# Output
Hello Riya! Are you from
India?

You might also like