Chapter 4 Functions
Chapter 4 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