Python:Functions: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| Line 1: | Line 1: | ||
[[category:Python]] | [[category:Python]] | ||
You can pass a function-name as paramter | |||
<syntaxhighlight lang='python'> | |||
def main(): | |||
outcome = function1('function2',par2) | |||
function(par2 = <value>, par1 = <value>) | |||
return | |||
def function1 (functiontocall, inpar2 = <defaultvalue>): | |||
result = functiontocall(inpar2) | |||
return result | |||
def function2 (inpar1, inpar2 = <defaultvalue>): | |||
codeblock | |||
return result | |||
main() | |||
</syntaxhighlight> | |||
=Build in function= | =Build in function= | ||
| Line 25: | Line 46: | ||
<syntaxhighlight lang='python'> | <syntaxhighlight lang='python'> | ||
def main(): | def main(): | ||
function(par1,par2) | outcome = function(par1,par2) | ||
function(par2 = <value>, par1 = <value>) | function(par2 = <value>, par1 = <value>) | ||
return | return | ||
def function ( | def function (inpar1, inpar2 = <defaultvalue>): | ||
codeblock | codeblock | ||
return | return result | ||
main() | main() | ||
</syntaxhighlight> | </syntaxhighlight> | ||
;def function (*args,**kwargs): | ;def function (*args,**kwargs): | ||
:Take any number of positional arguments and put them in the [[Python:DataTypes#Tuple|tuple]] args | :Take any number of positional arguments and put them in the [[Python:DataTypes#Tuple|tuple]] args | ||
:Take any number of keyword arguments and put them in the [[Python:DataTypes#Dictionary_or_dict|dict]] kwargs | :Take any number of keyword arguments and put them in the [[Python:DataTypes#Dictionary_or_dict|dict]] kwargs | ||
Revision as of 12:44, 24 January 2020
You can pass a function-name as paramter
def main():
outcome = function1('function2',par2)
function(par2 = <value>, par1 = <value>)
return
def function1 (functiontocall, inpar2 = <defaultvalue>):
result = functiontocall(inpar2)
return result
def function2 (inpar1, inpar2 = <defaultvalue>):
codeblock
return result
main()
Build in function
List of all build in functions [1]
- len(obj1)
- Return the number of elements in obj1 (string, list, tuple, set)
- max(obj1)
- Return the largest item in obj1
- min(obj1)
Return the smallest item in obj1
- input([prompt])
- Read and return input from standard input. Display 'prompt' first when provided.
- int(str[,base=x)])
- Convert str to an integer. Base = 2 converts bit-string presentation ('010101').
Self defined functions
Arguments are positional but can be passed by keyword too.
def main():
outcome = function(par1,par2)
function(par2 = <value>, par1 = <value>)
return
def function (inpar1, inpar2 = <defaultvalue>):
codeblock
return result
main()