Python Functions

128

Python functions or functions in python are pieces of code designed for specific purposes. The main idea behind creating functions in python is that if a piece of code is to be run again and again, so instead of typing that too many times, a function can be declared that can be called whenever there is a need for that code.

function in the python
function in the python

Now, we will learn about how to create a function in python –

How to create a function in Python

We can create a Python function using the def function.

Example –

def function():
print(“Welcome to Python Programming”)

Calling a Python Function

Once a function is created, it can be called any number of times just by the name of the function followed by parenthesis.

Example  –

def func():
print(“Welcome to Python Programming”)
fun()

Output –

Welcome to Python  Programming

Functions with parameters –

If we need some variables of anything inside a function, we can take this with the help of arguments. Arguments are mentioned inside the parenthesis after the function name. We will understand this better with the help of the example given below –

def add( a:int, b:int) -> int
c = a+b
return c
a,b = 4,6
ans = add(a,b)
print(“The sum is {ans}.”)

Output –

The sum is 10.

Different types of arguments in python functions–

Python supports various types of arguments that can be passed during calling a function in python.

Default Arguments

A default argument is a parameter that assumes a default value. This is done if the argument is not passed during a function call. Example –

def Func(x,y = 10):
print(“x is :”, x)
print(“y is :”, y)
myFun(10)

Output –

x : 10
y : 50

Keyword Arguments

In this type, the argument name is passed with the values.

def student(name1,name2):
print(name1,name2)
student(name1 = “john”, name2 = “rohan”)
student(lastname = “Practice”, firstname = “Geeks”)

Positional Arguments

Those arguments in which position matters are called positional arguments.

call_func(arg1, arg2, arg3);

We will understand this with the help of the example given below –

def details(name, age):

print(f"Hello I am {name}. I am {age } years old.")

info("Harshit", 20)

Output –

Hi, my name is Harshit. I am 20 years old.

But, if we change the order of these parameters, it will cause an error in the program.

info(20, "Harshit")

Output –

Traceback (most recent call last):

  File "example.py", line 5, in <module>

    info(23.0, "Alice")

  File "example.py", line 3, in info

    print(f"Hi, my name is {name}. I am  {age * 365.25} days old.")

TypeError: can't multiply sequence by non-int of type 'float'

Arbitrary Arguments

Sometimes, a case arises where the programmer does not know how many arguments will be passed to a function. In this case, we need to make a function that will run fine in any number of arguments.

def add(*num):
sum = 0;
for n in num:
sum = sum + n;
print("Sum:", sum)
add(4,5,6,7,8)
add(1,2,3,5,6,7,8)

Output –

30
36

Return Statement in Python

When a function ends, the last statement is used as a return statement that returns the specified data item to the caller.

The syntax of the return statement is –

return[expression_list]

We will understand it with the help of the example given below –

def add( a:int, b:int) -> int
c = a+b
return c
a,b = 4,6
ans = add(a,b)
print(“The sum is {ans}.”)

Explanation –

In this, after the function is executed, the sum of the two numbers is returned to the place from where the function was called. Then the sum is assigned to the variable ‘ans’, which is printed in the next line.

Output –

x : 10
y : 50

Pass by Reference or pass by value

Pass by Value

When we pass a variable to a function, a reference to the object is created. Passing a parameter is similar to passing a reference in Java.

Example –

def func(x):

x[0] = 25

list – [10,15,20,25,30]

func(list)

Output –

[25, 15, 20, 25, 30]

Explanation –

In this, the reference if the array is passed. And, if any change is made in reference, the change is made to receive the list/variable.

Pass by Reference

But, in the case of Passing by reference, the actual value is not changed when any change is made in the function.

Example –

def namechange(mylist):
mylist = [5,10,15,20]
mylist  = [1,2,3,4]
namechange(mylist)
print(mylist)

Output –

1,2,3,4

In this article, we have learned about the use of functions in Python. Using functions for some tasks makes it very easy rather than typing the codes again and again. We can also call a function within a function. Such a kind of nesting of functions is called recursion. Moreover, there are many pre-defined functions that are included in specific libraries that make the work easier for its users. The availability of large libraries in Python also makes it one of the most widely used languages.

Related Quiz:

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.