Python-Ref > Basic usage > Functions > Using functions
 
 

^^->
Klíčová slova
Moduly
Knihovní funkce

Using functions

Howto call a function
To use a function simply write its name and add any arguments behind it in parenthesis separated by a comma.
Expand/Shrink
Zdroj: (func1-1.py)
  1   a = range( 10)   # call the builtin function range with one attribute
  2   print a
  3   
  4   b = sum( a)    # function sum returns the sum of all items in a
  5   print b
stdout:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
45
Doba běhu: 21.2 ms
Many useful functions are available in Python inside various modules.
Expand/Shrink
Zdroj: (func1-2.py)
  1   import math            # import the module math
  2   print math.sin( 0.5)   # call the function sin from module math
stdout:
0.479425538604
Doba běhu: 21.3 ms
Some functions do not have arguments at all.
Expand/Shrink
Zdroj: (func1-3.py)
  1   import time            # import the module time
  2   print time.time()      # call the function time from module time
stdout:
1204113571.32
Doba běhu: 21.5 ms
In this case it is necessary to provide empty parenthesis after the name of the function. Without it the statement is not a function call but simply a reference to the function.
Expand/Shrink
Zdroj: (func1-4.py)
  1   import time            # import the module time
  2   print time.time()      # call the function time from module time
  3   print time.time        # the function time from module time
stdout:
1204113571.34
<built-in function time>
Doba běhu: 21.6 ms