Python Functions

The function is a structure of a program which supports the re-usability. These functions enable suitable modularity for the software in an individual action. Python supports built-in functions like Print (). It enables the clients to generate the function of our own. These are called user-defined functions.

Rules for representing a function

The function name must begin with a def keyword pursue by a function name and parenthesis.

All the arguments and input parameters must be located inside the parenthesis.

The functional program within the function must begin with the colon :

It can exit the function, and we can utilize the return (this can assign object return to the caller)

Syntax

def. func_name():

statements ()

return

Example

def sum(x,y):

z=x+y

return z

sum (10, 20)

Output

30

Function calling (or) function invoking

The definition of the function would provide the name and define the values/ parameter that should be contained in a function. Once the function has provided the basic mechanism, it can be implemented by calling from the other functions. Besides, this can also be known as from the python prompt.

Syntax

Def function_name ():

Print ()

Function_name ()

Example

Def first ():

print (‘this is the first function’)

first ()

Arguments

These are the values passed in a function. There are the following types of arguments are as follows:

Default arguments

These arguments support a default value if nothing is kept in the function call. These have to be represented in the function definition.

Keyword arguments

These arguments are associated with the calls. Due to parameter, name-caller recognize the arguments. This enables skipping arguments.

Required arguments

These are those arguments that are passed to the function in the right series as per their situations.

Variable-length arguments

In some methods, we require to write functions that accept more arguments than they specified. These arguments are known as these arguments as a variable-length argument.

Scope of the variable

The variables declare this have multiple scopes.

Local variable

The variables that can be approached in a function, where it is declared. It cannot be approached outside the function.

Example

def first():

a = ‘We are the local variable’

print (a)

first ()

Output

We are the local variable

Global Variable

This is unlike the local variable. This can be accessed worldwide. The variables that are declared ones can be approached a few functions.

Example

Def. f():

s= ‘We’ are the local function. We override the global function

print (s)

a= ‘We are the local function, we cannot access the outside the function f

print (a)

# Global scope

b= “Since, We are the global function, print S is called before the function, and first expected”

Print

f ()

a= ‘i love python’

Print (a)

Output

Since, We are the global function, print S is called before the function, and first expected we are the local function. We overrided the global function

We are the local function, and we cannot access the outside the function f

I love python

What are Doc strings?

It supports an appropriate method of relating records, with python modules, classes, functions, and methods. doc_ strings can be approached by __doc__ attribute.

How to define a docstring?

The docstring line must begin with a capital letter. The first line must be a short definition, and we must not write the object name.

Example

def my_function ():

“””Illustrate docstrings and does nothing really.”””

return None

print (“Using __doc__:”)

print (my_function.__doc__)

Output

Using __doc__:

Demonstrate docstrings and does nothing really.

Parameter Passing

There are two most general methods of passing a parameter to a function.

Call by Value

This method is used in C, C++ or Java but not utilized in Python. In call by value, the values of actual arguments are replica to function’s formal arguments, and both types of arguments are saved in individual memory locations.

Call by Reference

The functions are known by reference in Python, which defines all the modification implemented to the inside the function reflected in an actual argument.

Recursive Functions

The recursive function is a function that calls itself in its definition. For instance the mathematical function, factorial, represented by factorial(n) = n*(n-1)*(n-2)*…*3*2*1. can be defined as:

def factorial(n):

#n here should be an integer

if n == 0:

return 1

else:

return n*factorial(n-1)

Enroll Yourself : Python Training In Delhi

Copyright 1999- Ducat Creative, All rights reserved.