Python – Functions

Python – Functions: A function is a set of statements that runs only when they are called from somewhere else in the programme. Functions allow the same code to be reused for multiple inputs, saving time and resources. The print function is one of the numerous built-in functions in Python (). User-defined functions are functions that a user can build on their own.

Create Function

A function is defined in Python by using the def keyword followed by the function’s name, parentheses holding the function’s parameter(s) if it has any, and the function definition.

Call Function

After declaring the function, it may be called from anywhere in the programme by using its name followed by parentheses holding the function’s parameter(s), if any are available.

Syntax

#Defining function
def function_name(parameters):
    statements

#Calling function
function_name(parameters)

Example: A function with no parameter

A function HelloWorld is constructed in the example below to print Hello World! It doesn’t require any arguments to run.

def HelloWorld():
  print("Hello World!")

HelloWorld()

The output of the above code will be:

Hello World!

Parameter

A parameter (sometimes referred to as an argument) is a variable that is used to transfer data within a function. The function in the preceding example has no parameters. A user, on the other hand, can build a function with one or more arguments. The function can utilise the value of an argument to get the desired outcome.

Example: A function with a parameter

The function MyFunction is built in the example below, and it only requires one parameter to run.

def MyFunction(name):
  print("Welcome to Python Programming! " + name +".")

MyFunction("John")
MyFunction("Marry")
MyFunction("Sam")

The output of the above code will be:

Welcome to Python Programming! John.
Welcome to Python Programming! Marry.
Welcome to Python Programming! Sam.

Default Parameter Value

At the time of function creation, a default value might be set to a parameter. When a function is called without an argument, the default value is used.

Example: A function with a default value parameter

The function MyFunction is built in the example below, and it only requires one parameter to run. If the argument isn’t given, the parameter’s default value is used.

def MyFunction(name = "Dude"):
  print("Welcome to Python Programming! " + name +".")

MyFunction()
MyFunction("John")

The output of the above code will be:

Welcome to Python Programming! Dude.
Welcome to Python Programming! John.

Example: Passing iterable in a function

The following example shows how to send an iterable as a parameter to a function.

def MyFunction(num):
  for i in num:
    if (i % 2 == 0):
      print(i ," is an even number.")
    else:
      print(i ," is an odd number.")

MyFunction(range(1,6))

The output of the above code will be:

1  is an odd number.
2  is an even number.
3  is an odd number.
4  is an even number.
5  is an odd number.

Function to Return Values

To return values, you can use a function. Consider the following example:

Example: A function with a default value parameter

The function MyFunction is constructed in the example below, and it has two parameters to execute: num and multiplier. The multiplier parameter’s default value is 2. If the multiplier argument is not specified, the function’s default value will be used. The product of these two inputs is returned by the function.

def MyFunction(num, multiplier = 2):
  x = num * multiplier
  return x 

print(MyFunction(5, 3))
print(MyFunction(10))
print(MyFunction(25, 4))

The output of the above code will be:

15
20
100

Function with Named Parameter

Parameter values can also be provided to functions in Python using the key = value syntax. The order of the parameters is irrelevant in this case.

Example:

The following example shows how to pass arguments in a key=value pair.

def MyFunction(num1, num2):
  print(num1/num2)

MyFunction(num1 = 5, num2 = 10)
MyFunction(num2 = 10, num1 = 5)

The output of the above code will be:

0.5
0.5

Unknown number of Parameters

Use * in front of the parameter name if there is no information about the number of parameters that will be supplied to the function. When you add * to a parameter, it becomes iterable, allowing you to store numerous values that can be iterated through or retrieved using the index number. Consider the following example:

Example: Function to multiply an unknown number of parameters

In the example below, a function named MyFunction is constructed that may be utilised when the number of parameters is unknown.

def MyFunction(*num):
  FinalValue = 1;
  for i in num:
    FinalValue = FinalValue * i
  return FinalValue

print(MyFunction(2, 3))
print(MyFunction(2, 3, 5))

The output of the above code will be:

6
30

Also Read: Java – Encapsulation

Leave a Reply

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