In this article, Python functions class12 you will get detailed notes about function types, return values, and the scope of the function variables.
Python functions class12- function types
Python supports three types of functions:
- Built-in Functions: Pre-defined functions of Python such as input(), int(), max(), len() etc.
- Functions defined in modules: Functions defined in particular modules, can be used when the module is imported. A module is a container of functions, variables, constants, and classes in a separate file that can be reused. The module can be imported in two ways
- import statement: Used to import the entire module. EX. import math
- from statement: import all functions or the selected one. EX. from random import randint
- User-Defined Functions: Function created by the programmer
User-Defined Functions – Python functions class12
In the above program, two functions were created
The simple_interest() function receives three parameters p,r and n. main() function prompts to input these values and is accepted as an argument in calling simple_interest() function.
Parameters and Arguments in Function
Parameters are the values provided at the time of function definition. For Ex. p,r and n.
Arguments are the values passed while calling a function. For Ex. princ_amt, r, n in main().
Types of Arguments:
Python supports four argument types:
- Positional Arguments: Arguments passed to a function in correct positional order, no. of arguments must match with no. of parameters required.
- Default Arguments: Assign default to value to a certain parameter, it is used when the user knows the value of the parameter, default values are specified in the function header. It is optional in the function call statement. If not provided in the function call statement then the default value is considered. Default arguments must be provided from right to left.
- Key Word Arguments: Keyword arguments are the named arguments with assigned values being passed in function call statement, the user can combine any type of argument.
- Variable Length Arguments: It allows the user to pass as many arguments as required in the program. Variable-length arguments are defined with * symbol.
def sum(*n): total=0 for i in n: total+=i print(“Sum = “, total) sum() # Calling function sum() # o/p sum=0 sum(10) #o/p sum=10 sum(10,20,30,40) #o/p sum=100
Rules for combining three types of arguments
1. An argument list must contain positional arguments followed by any keyword argument.
2. Keyword arguments should be taken from the required arguments preferably.
3. Value of argument can’t be specified more than once.
cal_discount(amt=500,rate=10) or cal_discount(rate=10,amt=500)
Python functions class12-Returning values from function
A function in Python may or may not return a value. A function that does not return values is called a void function. A result will be printed in the same function.
A function that returns a value is a function that displays the calculated result. The value returned to a specific variable. The result will be printed in some other function.
Python functions class12 – Scope of Variable
A variable scope refers to access location of a variable.
There are two kinds of scopes:
Global Variable: A variable that is declared in top-level statements is called a global variable. To access the value of a global variable user needs to write a global keyword in front of the variable in a function.
Local Variable: A name declared in a specific function body is called a local variable.
Mutable/immutable of arguments/parameters & function calls
All variables in Python store the memory address of the actual object, not the value itself. Although mutable and immutable objects and variables are handled differently during working with function arguments.
Immutable objects are those objects whose values cannot be changed. They allocate new memory whenever the value is changed.
New memory allocated for x when value assigned x=45. Every time when value is changed memory address also gets changed. When new object is created old object will be discarded and memory cells make free for use.
Passing immutable type value
Passing mutable type value (List)
As you know list, dictionary etc are mutable data types in python.
Consider following program:
Thank you fore reading the article. Share your valuable feedback in comment section.
If you are looking for questions from function follow this link:
Share this article with your friends and ask your doubt in comment section.
That all from python functions class12.
Thank you for reading this article.