Python Function

  • A function is a block of code, it runs when a function is called.
  • Using def Keyword we create the function.
  • Function is also called Method.

Types of function

There are two types of python functions.

  • User define 
  • Predefine

Creating a Function

  • Creating a User-defined function using the def keyword.
  • Create user define functions in four different ways.
user define function

Take Nothing Return Nothing

Example

Take Nothing Return Nothing

def add():
 print("Enter the first number")
 x=int(input())
 print("Enter the first number")
 y=int(input())
 z=x+y
 print("Your Add Result is ",z)

add()
Output
Enter the first number
5
Enter the first number
3
Your Add Result is 8

Take Something Return Nothing

  • Parameter and Argument: The terms parameter and argument can be used for the same thing: information that is passed into a function.
  • Passing Arguments in function definition time is known as formal arguments.
  • Passing Arguments in function calling time is known as Actual arguments.
Example

Take Something Return Nothing

def add(x,y):
 z=x+y
 print("Your Add Result is ",z)

print("Enter the first number")
x=int(input())
print("Enter the first number")
y=int(input())
add(x,y)
Output
Enter the first number
5
Enter the first number
3
Your Add Result is 8

Take Nothing Return Something

  • Return: Return keyword is used to return the value.
  • The function will return the value from where it is called
Example

Take Nothing Return Something

def add():
 print("Enter the first number")
 x=int(input())
 print("Enter the first number")
 y=int(input())
 z=x+y
 return z



z=add()
print("Your Add Result is ",z)
Output
Enter the first number
5
Enter the first number
3
Your Add Result is 8

Take something Return Something

Example

Take something Return Something

def add(x,y):
 z=x+y
 return z



print("Enter the first number")
x=int(input())
print("Enter the first number")
y=int(input())
z=add(x,y)
print("Your Add Result is ",z)
Output
Enter the first number
5
Enter the first number
3
Your Add Result is 8

Note: Predefine functions are learned in the predefine chapter click here