Functions in Python with Examples

In this article, We are going to cover functions in python with examples, user defined function in python, Parameterized Function in Python, Keyword Arguments in python function, Default argument in Python function.

What is Functions in Python ?

Function is a self contain block which execute when user calls it. Function is used to minimize the length of the program. It allows us to reuse common part of the code. It can be called anywhere in the program and anytimes. Function is sub program does a sub task. Function are implemented in program to modularize. Functions are a set of operations.

Modularize: This says divide a big problem into number of sub program. And each program now can be solve using a sub program that is function.

In python we can write function and they can divide into two different categories.

  • User defined function
  • Built-in function

User defined function in Python

A function as it is a sub program it can take also input. In python we define the user-define function using def keyword, followed by the function name. function must return value.

Syntax:

def functionname(parameter):
	#function code

Example of function in Python:

def displayname():
    print("FOSSTechNix")
displayname()

Output:

FOSSTechNix
Functions in Python with Examples 1

Parameterized Function in Python

Parameterized is the process of taking values or objects defined within a function o a method.

Syntax:

#defining the function with a single parameter
Def functionname(var):
	Print(“Welcome”+var)
#calling that function which is having a single parameter functionname(“value”)

Example of single parameterized function in Python:

def check(var1):
    var1= int(var1)
    if var1%2 ==20:
        print("Even Number")
    else:
        print("Odd Number")
num1= int(input("Enter a number"))
check(num1)

Output:

Enter a number:88
Even Number
Functions in Python with Examples 2

Multi parameterized function in Python

Syntax:

def functionname(var1,var2):
	print(“Welcome“+var1+” ”+var2)

Example of multi parameterized function in python:

def add(var1,var2):
    var1= int(var1)
    var2= int(var2)
    sum= var1+var2
    return sum
num1= input("enter the first value:")
num2= input("enter th second value:")
total= add(num1,num2)
print("total value:",total)

Output:

enter the first value:23
enter th second value:25
total value: 48
Functions in Python with Examples 3

Keyword Arguments in python function

In this you can send arguments with key = value syntax

Example of keyword arguments in python function:

def key(fname, lname):  
     print(fname, lname)  
                    
key(fname ='keyword', lname ='arguments') 

Output:

keyword arguments
Functions in Python with Examples 4

Default argument in Python function

In this argument that assumes a default value if value is not declare in the function call for that argument.

Example of default argument in python function:

def fun(a, b=10): 
    print("a: ", a) 
    print("b: ", b) 
fun(5)

Output:

a:  5
b:  10
Functions in Python with Examples 5

Python function real time scenario based Question:

Q. Create a function that accept name, bill amount and birth year if age of the candidate is above 60 it will get senior citizen of 60% discount and if age is below 20 it will get teenage discount 30% on the bill.

Answer:

def billing():
    name=input("enter name")
    byear=int(input("Enter birth year"))
    bill=int(input("enter bill amount"))
    age=2020-byear
    dis=0
    if(age>60):
        dis=bill*60/100
    elif(age<20):
        dis=bill*30/100
    else:
        dis=0
    totalbill=bill-dis
    print("The name is:" +name)
    print("The age is:" ,age)
    print("the bill amount is:" ,bill)
    print("The discount is:" ,dis)
    print("Bill after discount:" ,totalbill)
billing()

Output:

Enter name shweta
Enter birth year 1998
enter bill amount 2000
The name is: shweta
The age is: 22
the bill amount is: 2000
The discount is: 0
Bill after discount: 2000
Functions in Python with Examples 6

Conclusion:

We have covered functions in python with examples, user defined function in python, Parameterized Function in Python, Keyword Arguments in python function, Default argument in Python function.

Related Articles:

Loops in Python 3 with Examples

7 Python Operators with Examples

Python Introduction for Programmers [Part 1]

6 Python Conditional Statements with Examples

Array in python with examples

classes and objects in python with examples

Reference:

Python function official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

2 thoughts on “Functions in Python with Examples”

Leave a Comment

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

Share via
Copy link
Powered by Social Snap