FUNCTIONS
Agenda
What is Function and types of functions
Call a function
Significance of indentation
How to function return a value
Arguments in functions
Functions
In Python, function is a group of related
statements that perform a specific task.
Functions help break our program into
smaller and modular chunks. As our program
grows larger and larger, functions make it
more organized and manageable
Types of functions
Built-in Functions:The Python interpreter
has a number of functions that are always
available for use. These functions are called
built-in functions.
example: print() function prints the given
object to the standard output device (screen)
or to the text stream file
User defined Functions:Functions that we
define ourselves to do certain specific task
are referred as user-defined functions.
Some Built in Functions
Continue….
Continue…
Continue….
len:Return the length (the number of items)
of an object. The argument may be a
sequence (such as a string, bytes, tuple, list,
or range) or a collection (such as a dictionary,
set, or frozen set).
Continue….
Round:Return number rounded to ndigits precision
after the decimal point. If ndigits is omitted or
is None, it returns the nearest integer to its input.
Sorted:Return a new sorted list from the items in
iterable
Power:The pow() method returns x to the power of y
Sum:sum(iterable) sums the numeric values in an
iterable such as a list, tuple, or set.
sum(iterable) does not work with strings because
you can't do math on strings (when you add two
strings you are really using an operation called
concatenation).
Continue…….
Help:The help() function is your new best friend.
Invoke the built-in help system on any object and
it will return usage information on the object.
User defined Functions
Syntax:
def function_name(parameters):
statement(s)
Advantages of user defined functions
User-defined functions help to decompose a
large program into small segments which
makes program easy to understand, maintain
and debug.
If repeated code occurs in a program,function
can be used to include those codes and execute
when needed by calling that function.
Programmars working on large project can
divide the workload by making different
functions.
Call a function
Once we have defined a function, we can call it from
another function, program or even the Python prompt.
To call a function we simply type the function name
with appropriate parameters.
Significance of Indentation
Indentation in a writing context typically
describes the space between the page border
and the start of the text.
In a programming context, it describes the
space between the edge of the editor and the
start of the code.
Python is unusual among programming
languages by having what is sometimes called
"significant whitespace", Where the amount of
space before the code starts affects the
structure of the program.
Code Blocks and Indentation
Consider the if-statement from our simple
password-checking program:
Continue…..
The lines print('Logging
on ...') and print('Incorrect password.') are two
separate code blocks.
To indicate a block of code in Python, you must
indent each line of the block by the same
amount.
The two blocks of code in our example if-
statement are both indented four spaces, which
is a typical amount of indentation for Python.
In Python, it is required for indicating what
block of code a statement belongs to. For
instance, the final print('All done!') is
not indented, and so is not part of the else-block.
How to function return a value
Functions that return values are sometimes
called fruitful functions.
In many other languages, a function that
doesn’t return a value is called a procedure.
But we will stick here with the Python way of
also calling it a function, or if we want to
stress it, a non-fruitful function.
Example
The square function will take one
number as a parameter and return
the result of squaring that number
Arguments in Functions
You can call a function by using the following
types of formal arguments:
1. Required arguments
2. Keyword arguments
3.Default arguments
4.Variable-length arguments
Required arguments
Required arguments are the arguments passed
to a function in correct positional order.
Here, the number of arguments in the function
call should match exactly with the function
definition.
To call the function printme(), you definitely
need to pass one argument, otherwise it gives a
syntax error as follows
Required arguments
Keyword arguments
Keyword arguments are related to the function
calls. When you use keyword arguments in a
function call, the caller identifies the arguments by
the parameter name.
This allows you to skip arguments or place them
out of order because the Python interpreter is able
to use the keywords provided to match the values
with parameters.
Continue….
You can also make keyword calls to the
printme() function in the following ways –
Continue…
The following example gives more clear picture.
Note that the order of parameters does not matter
Default arguments
A default argument is an argument that assumes a
default value if a value is not provided in the
function call for that argument.
The following example gives an idea on default
arguments, it prints default age if it is not passed
Continue…
Variable length arguments
You may need to process a function for more
arguments than you specified while defining
the function
These arguments are called variable-length
arguments and are not named in the function
definition,unlike required and default
arguments.
Continue….
Syntax for a function with non-keyword
variable arguments
An asterisk (*) is placed before the variable name
that holds the values of all no keyword variable
arguments.
This tuple remains empty if no additional
arguments are specified during the function call
Continue…..