Chapter 2 : Functions in Python
A function is a block of code which only runs when it is called.
Features / Advantages of using functions :
Write once and run any where and any number of times
Due to use of functions source code will reduce and affects execution
time i.e program will take less time to execute.
Classification of functions :
Built in Functions User defined Functions
Non parameterized functions Parameterized functions
Positional Para.. Keyword Para.. Default Para..
Built in Functions: ( eg sqrt(),pow(),abs(),speep(),floor(),ceil() etc )
Built in functions are also called as ready made functions.
To access built in functions respective module(container of functions) need
to import in program
User defined Functions:
User can create his own functions such a functions are called as user
defined functions.
Syntax for defining user defined functions
def function_name():
statement1
statement 2
……….
……….
function_name() -------------- function call
Or
Parameterized functions in python:
Python function accepts input information through parameters.
User may pass one or more than one parameters to function
Note: By default, a function must be called with the correct number of
arguments. i.e if your function expects 2 arguments, you have to call the
function with 2 arguments, not more, and not less.
Different ways of passing parameters to functions:
1. Positional Arguments
Note: by interchanging positions of parameters function will produce
different results and which is key drawback of of this method.
2. Keyword Arguments:
3. Default argument of function:
Passing List as an argument to function: