HOW TO USE FUNCTIONS IN PYTHON?

410 viewsPYTHONPython
0

HOW TO USE FUNCTIONS IN PYTHON?

Saraswat World Changed status to publish June 16, 2023
0
WHAT IS A FUNCTION IN PYTHON?

A function is a named sequence of statements or a block of code that only runs when it is called by its name.

We can pass data, known as parameters, into a function. On calling a function it will execute the block of code and can return data as a result.

Functions can be defined in any order. Functions must only be defined prior to using or calling during program execution.

Creating a Function

A function is defined in Python using the def keyword

def function_name():
  print("Hello")

Calling or executing function

We can call a function by using the function name followed by parenthesis:

def function_name():
  print("Hello")
function_name()

output

Hello

Arguments in Functions

We can pass the information in the function as arguments or parameters. Arguments are specified after the function name, inside the parentheses. We can add multiple arguments, each one separated by a comma, in the function.

As shown in the following example, a function named func1 is having one argument (firstname). At the time of calling the func1, we are passing along a first name, which is used inside the function to print the full name:

def func1(firstname):
  print(firstname + " Sharma")

func1("Rakesh")

output

Rakesh Sharma

Arbitrary Arguments, *args

If we want to pass an unknown no. of parameters or arguments, add a * before the parameter name in the function definition. By using *before the argument or parameter, our function will receive a tuple of arguments and can access the items accordingly.

def func1(*fruits):
  print("My favourite fruit is " + fruits[2])

func1("apple", "banana", "cherry")

output

My favourite fruit is cherry

Keyword Arguments

We can also send arguments with the key = value syntax. This way the order of the arguments does not matter.

def func1(f3,f2,f1):
  print("My favourite fruit is " + f3)

func1(f1="apple", f2="banana", f3="cherry")

output

My favourite fruit is cherry

Arbitrary Keyword Arguments, **kwargs

Arbitrary keyword Arguments can be passed into the function by adding two asterisk** before the parameter name in the function definition. This way the function will receive a dictionary of arguments, and can access the items accordingly

def my_func1(**namelist):
  print("His last name is " + namelist["lname"])

my_func1(fname = "Rakesh", lname = "Sharma")

output

His last name is Sharma

Default Parameter Value

The default parameter with its value is provided in the function definition and when the function is called without an argument, then the default value is used to pass in the function as a parameter.

def my_func1(fruit = "mango"):
  print("My favourite fruit is " + fruit)

my_func1("apple")
my_func1("bannana")
my_func1()

output

My favourite fruit is apple
My favourite fruit is bannana
My favourite fruit is mango

Return Values

Return keyword is used to let the function return the data or values.

def my_func1(x):
  return 2 * x

print(my_func1(2))

output

4

The pass Statement

The function definition cannot be empty, but if there is no content, we can put in the pass statement to avoid getting an error.

def myfunction():
  pass

Recursion

Recursion means a function calls itself, such functions are also known as recursive functions.

def func_recursion(i):
  if(i > 0):
    result = i + func_recursion(i - 1)
    print(result)
  else:
    result = 0
  return result

print("Output of Recursion")
func_recursion(3)

output

Output of Recursion
1
3
6
Saraswat World Changed status to publish June 16, 2023
Write your answer.

Posted

in

Tags:

Comments

Leave a Reply

A Tribute to Dilip Kumar