Tamilnadu Samacheer Kalvi 12th Computer Science Notes Chapter 7 Python Functions Notes
A named blocks of code that are designed to do one specific job is called
Block in python:
A block is one or more lines of code, grouped together so that they are treated as one big sequence of statements while execution.
Main advantages of function:
- It avoids repetition and makes a high degree of code reusing.
- It provides better modularity for your application.
A Function which calls itself is called as Recursive Function.
Different types of function:
- User-defined functions
- Built in functions
- Lambda functions
- Recursive functions
1. User defined function:
Functions defined by the users themselves are called User defined functions.
def<function_name ([parameter 1, parameter 2 ]) > :
< Block of statement >
return < expression / None>
print(“Welcome to Python”)
2. Built-in functions:
Functions which are using Python libraries are called Built-in function.
3. Lambda function:
- Lambda function is mostly used for creating small and one-time anonymous function.
- Lambda functions are mainly used in combination with the functions like filterQ, map() and reduceQ.
Syntax of Lambda function (Anonymous Functions):
lambda [argument(s)]: expression
4. Recursive function:
- A recursive function calls itself. Imagine a process would iterate indefinitely if not stopped by some condition! Such a process is known as infinite iteration.
- The condition that is applied in any recursive function is known as a base condition.
- A base condition is must in every recursive function otherwise it will continue to execute like an infinite loop.
Scope of variable:
- Scope of variable refers to the part of the program, where it is accessible,
- The scope holds the current set of variables and their values.
- The two types of scopes are – local scope and global scope.
A variable declared inside the function’s body or in the local scope is known as local variable.
- A variable, with global scope can be used anywhere in the program.
- It can be created by defining a variable outside the scope of any function or block.
Parameters and arguments:
- Parameters are the variables used in the function definition.
- Arguments are the values we pass to the function parameters.
- Arguments are used to call a function.
- There are primarily four types of functions namely :
- Required arguments
- Keyword arguments,
- Default arguments S Variable-length arguments
- “Required Arguments” are the arguments passed to a function in correct positional order.
- The number of arguments in the function call should match exactly with the function definition.
- Atleast one parameter to prevent syntax errors to get the required output.
- Keyword arguments will invoke the function after the parameters are recognized by their parameter names.
- The value of the keyword argument is matched with the parameter name and so, one can also put arguments in improper order (not in order).
In Python the default argument is an argument that takes a default value if no value is provided in the function call.
- In some instances it is needed to pass more arguments that have already been specified.
- These arguments are not specified in the function’s definition and an asterisk (*) is used to define such arguments.
- These types of arguments are called Variable-Length arguments.
Composition in functions:
- The value returned by a function may be used as an argument for another function in a nested manner is called