How to Use def() function in python !

Today you will learn about def() function in “How to Use def() function in python !”

Defining Functions

A function definition is an executable statement. Its execution binds the function name in the current local namespace to a function object (a wrapper around the executable code for the function). This function object contains a reference to the current global namespace as the global namespace to be used when the function is called.
The function definition does not execute the function body; this gets executed only when the function is called.
When one or more top-level parameters have the form parameter = expression, the function is said to have “default parameter values.” For a parameter with a default value, the corresponding argument may be omitted from a call, in which case the parameter’s default value is substituted. If a parameter has a default value, all following parameters must also have a default value — this is a syntactic restriction that is not expressed by the grammar.

Type of Function:

  • Built-in-Functions into Python.
  • User-Defined Functions themselves.

    So We learn about user defined Functions..

Syntax of def() function

def function(parameter):
“””Docstring”””” ## use for function comment
statement() ## with indented 4 space

now you show function definition that consists of the following components.

  • def = Start the Function Header.
  • function = unique identify the function.
  • parameter = value to a function. (optional)
  • colon : = end of the function header.
  • docstring = describe what the function does.
  • statement = valid python statements that make up the function body with indented 4 spaces.
  • return = optional return statement to return a value from the function.


def example():
example()  ## now this a my new function  when i again run example() then same output at same time



EXAMPLE – 2 With Parameter

def greet_with(name):
print(f”hello {name}”)
print(f”how are you {name}”)


hello atul
how are you atul

above you show a example with parameter and my parameter is name and when run the program then define the “atul” in instead name .

EXAMPLE – 3 With Docstring

def greet():
“””This is a docstring Example”””

Docstring to define a function comment and remember what the use for this function just like above Screenshot greet() is my function and docstring define greet function.

So, what to do when define a 2 parameter a function,below a another example.

EXAMPLE – 4 With 2 parameter

def greet_with(name,city):
print(f”hello {name}”)
print(f”im from {city}”)
greet_with(“Atul”, “Delhi”) ## here you show parameter is order wise if put in delhi instead to atul then my output is “hello delhi”


hello Atul
im from Delhi

so, What to do when put a value in parameter in multiple value.
when you run the program then you put a value like below:-

greet_with(city = “delhi”,name = “atul”)

and you got order wise value which you give in parameter

Return Statement

return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned.

EXAMPLE – 5 With use return statement.

def number():
a = 5
if a == 5:
return True
return False



Please note: return function use only when a create a new function

Now lets make a program with use all the learned function.

FINAL EXAMPLE (with make program)

def prime_checker(number):
is_prime = True
for p in range(2, number – 1):
if number % p == 0:
is_prime = False
if is_prime:
return “its a prime number”
return “it’s not a prime number”
n = int(input(“Check this number: “))



Check this number: 7
its a prime number

So today you will learned about “How to Use def() function in python !” .

Thank you ❤️

check out

Leave a Reply

Your email address will not be published. Required fields are marked *