Before we begin functions let us discuss some important points. . .
Type conversion
x=1000
str(x)
will give '1000' so the value cannot be changed because string is immutable in python
similarly see some type conversions below..
while using cmp(x,y) if it returns non zero indicates that strings/list/tuple are not equal.
FUNCTIONS
Functions can be created in two ways:
def() -Regular standard function
lambda() -anonymous function no function name.
import can be used to import file from terminal to python prompt
lets see a program...
the above program will print
in function
Hello World
test() will take zero arguments and return NULL
dir(str) -will give directory
help(str.index) -will give man page
help(str.index) -will give man page
The arguments can be called in a function using:
- Required arguments(The number of called function and calling function should be same)
- Keyword arguments
- Default arguments (add(x,y) or add(y,x))
- Variable arguments
the program will print :
hello in function 10 20
value=30
Keyword arguments
Required arguments
Default argument
In the above program default marks will be taken if it is not provided in function call.
Variable argument
In Python return function can return more than one function which C language cannot perform.The values are returned in tuple format.
to use global variable inside a block use global keyword
OUTPUT :
While using any kind of functions value is called as call by reference.
0 on: "Functions in Python"