Working with Functions in Python Notes for Class 12 Computer Science, Informatics Practices

Working with Function Notes Python Class 12

Functions in Python

In this section we will discuss about the function in Python. It will help the students having Computer Science / Informatics Practices in Class 11 and 12.

 

What is Function?

In Python, a function is a set of related statements that perform a specific task. Functions help to divide our program into smaller and more modular parts. As our programs become larger and larger, these features make them more organized and clear. It also avoids duplication and allows code to be reused.

 

Functions are sub programs of a program. It means a function is a part of a program that carries out some well defined task.

 

Types of Functions:

1. Built in Function

These are the pre defined functions having some specific work to perform.

For Example:

print ( ): print the statement or result itself (We don’t know how, we just use)

input ( ): Takes input from the keyboard (We don’t know how, we just use)

len(), min(), max(), type() etc. are also some built in functions in Python

 

2. Functions defined in Modules:

Library: A library is a bundle of code made to help you accomplish routine tasks more quickly. We just have to import the module in our program.

Example: math(), Random(), Pandas, numpy etc.

 

3. User defined Functions

These are the functions which are defined/written by the user to perform some specific task.

Syntax of user defined Function

def function_name(parameters):
	"""docstring"""
	statement(s)

 

Keyword def, used to mark the beginning of the function header.

Function name, used to uniquely identify the function. Function names follow the same rules for writing identifiers in Python.

We use it to pass values to the parameters (arguments) of the function.

The  colon (:) is used to mark the end of the function header;

The optional return statement is used to return a value from a function.

 

Creating a function in Python

def my_function():
    print("Hi this is python function")

 

Calling a function in Python

def my_function():
    print("Hi This is function")
my_function()

Output:

Hi This is function

> Argument: a value that is supplied to the function by user

> Return: A value that a function returns to its calling place. (will not display the result until you print the result)

 

There are many ways to create a user defined function

1. No Argument No return

2. with Argument No return

3. No Argument with return

4. with Argument with return

 

Now we will discuss one by one with the help of a python program to add two numbers.

i) No Argument No return

def Add():
     a= int (input(“Enter First Number”))
     b= int (input(“Enter Second Number”))
     c=a+b
     print (“The Sum of inputted Numbers is:”, c)
Add()
print(“ Executed”)

 

ii) with Argument No return

def Add(a,b):
     c=a+b
     print(“The Sum of inputted Numbers is:”, c)
num1=int (input(“Enter First Number”)) 
num2= int (input(“Enter Second Number”))
add(num1,num2) 

 

Remember: The variable in main program are differ from the variable in function definition i.e. a and b should be x and y. In this scenario the variable passed (x,y)will be called argument, and when it replace with the variable defined in the function will be called as parameter.

iii) No Argument with return

def Add():
       a=int (input(“Enter First Number”))
       b= int (input(“Enter Second Number”))
       c=a+b
       return c
x= add()
print (“The Sum of inputted Numbers is:”, x)

Note: { As return does not show the result we have to store the returned value on another variable x}

iv) with Argument with return

def  Add (a,b):
            c=a+b
            return c
a=int (input(“Enter First Number”))
b= int (input(“Enter Second Number”))
x= add(a,b)
print (“The Sum of inputted Numbers is:”, x)

Difference between Arguments and parameters

These two terms are very interchangeable, so it is not so important to know the difference. The terms they refer to are almost identical. However, in order to sound more professional, correct terminology is important. Variables that are in brackets when defining the function. When a method is called, the arguments are the data passed to the method’s parameters.

Function arguments : Arguments are used to pass information from the rest of the program to the function. This information return a result. There is no limit to the number of arguments that can be written. Depending on the type of function you’re performing, there might even be no argument.

Use commas to separate the arguments in a function. Take into account the number of arguments you put in a function call. The number of arguments must be exactly the same as the number of parameters.

 

In the example below, the variable name is the input parameter, where as the value, “Arnav”, passed in the function call is the argument.

 

def welcome(name):
    print("Hello! " + name + " Good Morning!!")
welcome("Arnav")

Output:

Hello!  Arnav Good Morning!!

 

Returning a Function

If you wish to return some values from the function to the rest of the program, you can use the return statement. As the name suggests, it returns a value without printing it. Executing this statement will cause the Python function to end immediately and store the value being returned into a variable.

 

How function  works

working with functions in Python

 

 

Scope and Lifetime of variables

 

When dealing with Python functions, you should also consider the scope of the variables. The code in a function is in its own little world, separate from the rest of the program. Any variable declared in the function is ignored by the rest of the function. This means that two variables with the same name can exist, one inside the function and the other outside the function. However, this is not a good practice. All variable names must be unique regardless of their scope.

 

Local variable: A variable that is defined within a function and can only be used within that particular function. With respect to the local variable, the area in a function is called “local area”.

Global variable: A variable that is defined in the main part of the programming and, unlike local variables, can be accessed via local and global areas.

 

In this article you learned functions in Python. For More on working with functions in python Check Here: Introduction to Python Module

Also Check: What is Random Module in Python? How to use random Module?