3.1 How to write a function ( v - 87 ).pdf
(
202 KB
)
Pobierz
How to write a function
•
We can write a function by using the keyword
def
followed by a function name.
•
The function name is user define and it is suggested to take a meaningful name while defining
•
•
•
•
•
•
•
•
•
•
a function
The rules for defining a function is same as giving variable names
Within the ( ) you can pass parameters to a function these Parameter are called
“Formal
parameter”
Parameters are called input to a function , a function can take multiple Parameter.
Parameters can be of any datatype
Returning values of the statements is Calle
“output”
You can call a function by using the function name and pass parameter in ( ) , these Parameter
are called
“Actual parameter”
The actual parameter values are copied into formal parameter which acts as input to a
function they are copied in the same position / order
When you call a functioning a result is returned you should place that result into another
variable (or) print it directly
If you don’t write return in function it'll return
NONE
So, every function returns whether you write it or not.
Syntax :
def fun_name ( par1 , par2, par3 ) :
Stat1
Stat2
……
……
return result
#Formal parameter
#Statements of function
#Returning result
#Calling a function
fun_name ( par1, par2, par3 )
return_value = fun_name ( apar1, apar2, apar3 )
A simple example to understand function.
Output :
•
The values in formal parameter acts as a pointer to actual parameter
•
therefore they'll be referring to the same thing
•
Lets understand this with an
Output:
•
In python object are always pass just like reference only , copy of an object will never be pass
Plik z chomika:
kendzior21
Inne pliki z tego folderu:
1. Section Introduction.mp4
(25995 KB)
2. What are Functions.mp4
(29622 KB)
28. Challenge Flattening Nested List.mp4
(37117 KB)
26. Challenge Minimum Variable Number.mp4
(26891 KB)
10.1 Iterators&Generators.pdf
(1043 KB)
Inne foldery tego chomika:
01. Introduction to Python
02. Python Installation and Setup
03. Python Datatypes
04. Operators and Expression
05. Conditional Statements
Zgłoś jeśli
naruszono regulamin