What is a Function in Python?

Ad Code

What is a Function in Python?

 

What is a Function in Python?

Function is slice of statements that together perform a specific task. You can divide up your code into separate functions. Functions avoid repetition, make code modular, and improve readability.

Syntax of a Function

def function_name(parameters):
    # block of code
    return result

Types of Functions in Python

1. User-defined functions

You define it using def.

2. Built-in functions

Like len(), print(), sum(), etc.

Creating a Function without argument

In Python a function is defined using the def keyword:

 

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

Calling a Function

To call a function, use the function name followed by parenthesis:

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

my_function()

Example1:

def myfunc(a,b):
    if a>b:
        print("Highest=",a);
    if b>a:
        print("Highest=",b);
 
x=int(input("enter a number"));
y=int(input("enter a number"));
myfunc(x,y);

Arguments

Information can be passed into functions as arguments.

Creating a Function with argument

def function_name(parameters):
    # What the function does goes here

    return result 

 Example:

def myfunc(r):
    a=(3.14*r*r);
    print("Area of circle =",a);
x=float(input("enter value of radius"));
myfunc(x);

Return values: 

The Keyword return is used to return back the value to the called function.

Area of Circle using function with return value.

 Example:

def myfunc(r):
    return (3.14*r*r);
        
x=float(input("enter value of radius"));
print("Area of circle =", myfunc(x));

Sometimes it is useful to have multiple return statements, one in each branch of a
conditional:

def absolute_value(x):
     if x < 0:
         return -x
    else:
         return x

Example:

def myfunc(a,b):
    if a>b:
       return a;
    if b>a:
        return b;
x=int(input("enter a number"));
y=int(input("enter a number"));
print("Highest number=",myfunc(x,y));

Since these return statements are in an alternative conditional, only one will be executed. As soon as a return statement executes, the function terminates without executing any subsequent statements. Code that appears after a return statement, or any other place the flow of execution can never reach, is called dead code.

# Write a Python function that takes two lists and returns True if they have at least one common member.

def cdata(list1, list2):
 for x in list1:
 for y in list2:
 if x == y:
 result = True
 return result
print(cdata([1,2,3,4,5], [1,2,3,4,5]))
print(cdata([1,2,3,4,5], [1,7,8,9,510]))
print(cdata([1,2,3,4,5], [6,7,8,9,10]))

Local and Global scope:

Local Scope:

A variable which is defined inside a function is local to that function. It is accessible from
the point at which it is defined until the end of the function, and exists for as long as the
function is executing

Global Scope:

A variable which is defined in the main body of a file is called a global variable. It will be
visible throughout the file, and also inside any file which imports that file.
  • The variable defined inside a function can also be made global by using the global statement.

def function_name(args):

# use local and global variables in same code
x = "global"
def f3():
 global x
 y = "local"
 x = x * 2
 print(x)
 print(y)

f3()

Output:
globalglobal 
local 
  • In the above code, we declare x as a global and y as a local variable in the f3(). Then, we use multiplication operator * to modify the global variable x and we print both x and y.
  • After calling the f3(), the value of x becomes global global because we used the x * 2 to print two times global. After that, we print the value of local variable y i.e local.




Post a Comment

0 Comments

Ad Code