Scripting

How Functions Work in Python

How_Functions_Work_in_Python

A function is a reusable block of code that performs a specific job. Also known as user-defined functions as they are defined by users. There are two kinds of functions, one which returns values while others only display output on the screen.

Functions in python

In this article, we will discuss those functions which display values on the screen.

Elements of a function

A function consists of three elements:

  1. Function Name
  2. Parameters
  3. A return value

1. Function Name

Each function has a specific name. For naming a function in python we need to follow these naming conventions.

Naming conventions for function:

1. It can begin with A – Z, a – z, and underscore (_).

2. A – Z, a – z, digits (0 – 9), and underscore (_) can be part of it.

3. A reserved keyword cannot be a function name.

2. Parameters

After the function name, we write parenthesis. In parenthesis, parameters are placed. Our function can have no parameter, or it can have one or more parameters. Parameters are values on which a function performs an operation and they are provided by a user. If we have more than one parameter, then insert a comma to separate them.

Syntax of the function definition:

To define a function, we write keyword def then function name followed by a parenthesis with or without parameters inside and semicolon. All statements that are part of the function body should be indented.

def functionname():

here is your function definition

How to Make a Function Call

Once you have completed the function definition, now it’s time to call the function. You call the function by its name with the argument list. You need to pass a value for every parameter, otherwise, the default value for that parameter will be considered.

def functionname():

here is your function definition

functionname()

Types of Functions

Based on parameters, we categorize functions as follows:

  1. Function without parameters
  2. Function with parameters

Function without parameters

A function that does not have any parameter. Here are a few examples.

Example 1:

def func():

print('Hello i am a function without parameters')

func()

Output:

Hello I am a function without parameters

Graphical user interface, text Description automatically generated

Output:

Text Description automatically generated

In Figure 1 we have defined a function func(). The body of function has a print statement. Calling the function displays a statement on the screen as shown in Figure2.

Example 2:

def show_main_menu():

print(

"1.Biology\n" +

"2.Pyhsics\n" +

"3.Chemistry\n" +

"4.Exit\n")

choice = input("Enter your choice: ")

if choice == "1":

print("You chose Biology")

elif choice == "2":

print("You chose Physics")

elif choice == "3":

print("You chose Chemistry")

elif choice == "4":

print("Goodbye")

else:

print("Wrong choice, Please Enter [1 to 4]\n")

ent = input("Press Enter to continue ...")

show_main_menu()

show_main_menu()

Output:
1.Biology

2.Pyhsics

3.Chemistry

4.Exit

Enter your choice: 2

You chose Physics

Text Description automatically generated

Output:

Text Description automatically generated

In example 2, Figure 3 we have defined a function show_main _menu(). The function displays a menu of options using the print command. For user choice, we have used the input function. The user selects a category number and the program prints a message according to the selected category as shown in Figure 4.

Function with parameters

Example1:

def table(num):

for i in range(1,11):

print(num,'X',i,'=',num*i)

table(12)

Output:

12 X 1 = 12

12 X 2 = 24

12 X 3 = 36

12 X 4 = 48

12 X 5 = 60

12 X 6 = 72

12 X 7 = 84

12 X 8 = 96

12 X 9 = 108

12 X 10 = 120

Text Description automatically generated

Output:

A screen shot of a computer Description automatically generated with low confidence

In Figure 5 we have defined a function table(num). We have used for loop to increment the value of ‘i’. By calling the function table (12) we get a table of 12 as shown in Figure 6.

Example 2:

def caldisc(amount):

if amount > 0:

if amount >= 100 or amount <= 199.99:

disc = amount * 0.05

owed = amount - disc

print("value of good {} discount given {} amount owed {}".format(amount, disc, owed))

elif amount >= 200:

disc = amount * 0.10

owed = amount - disc

print("value of good {} discount given {} amount owed {}".format(amount, disc, owed))

else:

print("Invalid Amount")

caldisc(150)

Output:
value of good 150 discount given 7.5 amount owed 142.5

Text Description automatically generated

Output:

In Figure 7 we have defined a function caldisc(amount) to calculate the values of goods purchased. If the value is £200 or more, a 10% discount is given and if the value is between £100 and £199.99, a 5% discount is given. A discount is subtracted from the value to find the amount owed. Then we have printed the value of goods, discount given and amount owed as shown in Figure 8.

Example 3:

def sum_numbers(x,y):

print('sum of two numbers', x,'and', y, 'is', x+y)

sum_numbers(12,22)

Output:

sum of two numbers 12 and 22 is 34

Graphical user interface Description automatically generated with medium confidence

Output:

In Figure 9, we have defined sum_numbers(x,y) function. It adds two numbers and displays the value as shown in Figure 10.

Similar Posts