USER DEFINED FUNCTION (1)
USER DEFINED FUNCTION (1)
FUNCTION
DECLARATION
DEFINITION
FUNCTION CALLING
# 1 keyword arguments
student('Donald', age=13)
Change the sequence of keyword arguments
You can change the sequence of keyword arguments by using their name in function
calls.
Python allows functions to be called using keyword arguments. But all the keyword
arguments should match the parameters in the function definition.
When we call functions in this way, the order (position) of the arguments can be
changed.
Example:
# both keyword arguments by changing their order
student(age=13, name='Kelly')
Output:
Student Details: Kelly 13
POSITIONAL ARGUMENTS
Positional arguments are those arguments where values get assigned to the
arguments by their position when the function is called.
For example, the 1st positional argument must be 1st when the function is
called. The 2nd positional argument needs to be 2nd when the function is
called, etc.
By default, Python functions are called using the positional arguments.
Example: Program to subtract 2 numbers using positional arguments.
def add(a, b):
print(a - b)
add(a,b) add(a,b)
add(50, 10)
# Output 40
add(10, 50)
# Output -40
add(50,10) add(10,50)
Note: If you try to pass more arguments, you will get an error.
In the positional argument number and position of arguments must be
matched.
If we change the order, then the result may change.
Also, If we change the number of arguments, we will get an error.
def add(a, b):
print(a - b)
add(105, 561, 4)
Output
TypeError: add() takes 2 positional arguments but 3 were given
def message():
print("Welcome to PYTHON")
# call function using its name
message()
Output
Welcome to PYTHON
# function
def course_func(name, course_name):
print("Hello", name, "Welcome to PYTHON ")
print("Your course name is", course_name)
# call function
course_func('John', 'Python')
Output
Hello John Welcome to PYTHON
Your course name is Python
# function
def calculator(a, b):
add = a + b
# return the addition
return add
# call function
# take return value in variable
res = calculator(20, 5)
Output
Addition : 25
CREATING A FUNCTION WITHOUT PARAMETERS AND RETURN VALUE
def fun():
str = "LEARNING PYTHON"
x = 20
return str, x; # Return tuple, we could also # write (str, x)
str, x = fun() # Assign returned tuple
print(str)
print(x)
Calling a function of a module
You can take advantage of the built-in module and use the functions defined in it.
For example, Python has a random module that is used for generating random
numbers and data. It has various functions to create different types of random data.
Let’s see how to use functions defined in any module.
First, we need to use the import statement to import a specific function from a
module.
Next, we can call that function by its name.
# import randint function
from random import randint
2)def add():
a=int(input("a="))
b=int(input("b="))
print(a+b)
add()
3)def add(a,b):
print(a+b)
a=int(input("a="))
b=int(input("b="))
add(a,b)
4) def add(a,b):
return (a+b)
result=add(5,8)
print(result)