WORKING WITH FUNCTIONS IN PYTHON
–>Instead of writing lengthy statements in a python program we will divide program into smallest units called functions.
–>Functions are the named units in a grp of python statements. We can call that function or invoke at diff parts of program at any number of times.
1.We can reduce program space
2.We can avoid ambiguity
3.A programmer can easily handle larger programs
4.Can easily rectify errors
FUNCTIONS IN PYTHON
–> It can have arguments
–>Can perform certain functionality
–>Can return some value
def is a keyword that defines function name.
square is a function name that id defined after keyword def.
the variable or identifier x is called argument or parameter must be defied within parenthesis
the colon after the function name indicates it requires a block
the return keyword will return the computed result
PYTHON PROGRAM ANATOMY
–>CALLING/INVOKING/USING F’N IN PYTHON:-
We can call a function at any where in the program in different types:
1.Can use value as an argument
2.Can use variable as an argument
3.We can use in a expression
4.We can call in another statement
PYTHON FUNCTION TYPES
- BUILT IN FUNCTIONS: These are pre-defined functions in Python. We can use these functions anywhere in the program. eg. print(),input(),int(),len()….etc
- FUNCTIONS DEFINED IN MODULES: These functions or methods are already implemented in python modules. in order to use those functions we should impot the respective module. For example if we want to use ceil() we have to import math module.
- USER DEFINED FUNCTIONS: These functions are defined by programmer itself while writing python program. The lifespan of this functions are only within the program.
**FUNCTION HEADER: It starts with the keyword def and ends with colon
**PARAMETERS: We can define or call functions with or without arguments and it may or may not return values
**INDENTATION: The block which is indented after function header
**FUNCTION BODY: A function body may have single or multiple statements will be considered as a separate unit called block
While executing statements python give priority to the non-indented statements after that it will execute indented blocks. The main segment i.e., main is stored in a built in variable called name. This variable is pre defined so that we can directly print the value.
ARGUMENTS AND PARAMETERS
Technically the variables which are ready to receive values are called “parameters”
The values or variables which are being sent are called “arguments”
The parameters in the function header is called as formal parameters or formal arguments
The arguments in the function called is called as actual arguments or actual parameters
We can pass parameters in 3 different ways:
- Positional parameters: The parameters in the function header will be match with the function call arguments. Their position and the values which are allocated will be in an order
- Default parameters: If we want to fix some values throughout a program we can use default parameters. If the programmer will not pass any arguments the system will consider the default values. If the programmer has given new parameters the system will ignore default values during execution
The main important rule while defining default parameter is “No value should be given a default parameter unless the parameters appearing on the right side have their default arguments”
3.Keyword or named parameters: Keyword arguments which are assigned with values. If the programmer changes the position of parameters it will not change the values
SCOPE OF A VARIABLE
- Global variable: It will be declared in the main segment and can be accessible and usable throughout the program including functional blocks
- Local variable: It will be declared in the main segment and can be accessible and usable throughout the program including functional blocks
When u access a variable within the program or function python follows name resolution rule.it is known as LEGB(local enclosing global built-in )rule
- Local environment: Variable with the same name than python uses its value.
- Enclosing environment: Whether there is a another variable with the same name in the outside of a function
- Global environment: Whether there is a variable with the same name in the main segment
- Built-in environment: If there is no such variable exist in the program interpreter go and search in the built in variables and libraries