pytutorial/python_basics/functions
David Rotermund 6bb5cd9adf
Create README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-13 14:43:56 +01:00
..
README.md Create README.md 2023-12-13 14:43:56 +01:00

Functions

{:.no_toc}

* TOC {:toc}

The goal

Questions to David Rotermund

Most simple function

These three functions are equivalent:

def my_function():
    pass

def my_function():
    return

def my_function():
    return None

return leaves the current function call with the expression list (or None) as return value.

There is a return at the end, even if you don't put it there.

def

funcdef                   ::=  [decorators] "def" funcname [type_params] "(" [parameter_list] ")"
                               ["->" expression] ":" suite
decorators                ::=  decorator+
decorator                 ::=  "@" assignment_expression NEWLINE
parameter_list            ::=  defparameter ("," defparameter)* "," "/" ["," [parameter_list_no_posonly]]
                                 | parameter_list_no_posonly
parameter_list_no_posonly ::=  defparameter ("," defparameter)* ["," [parameter_list_starargs]]
                               | parameter_list_starargs
parameter_list_starargs   ::=  "*" [parameter] ("," defparameter)* ["," ["**" parameter [","]]]
                               | "**" parameter [","]
parameter                 ::=  identifier [":" expression]
defparameter              ::=  parameter ["=" expression]
funcname                  ::=  identifier

pass

pass_stmt ::=  "pass"

pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed

return

return_stmt ::=  "return" [expression_list]

return may only occur syntactically nested in a function definition, not within a nested class definition.

If an expression list is present, it is evaluated, else None is substituted.

return leaves the current function call with the expression list (or None) as return value.

When return passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the function.

Return values

No return value

As we stated above these functions all return None:

def my_function():
    pass

def my_function():
    return

def my_function():
    return None

One return value

These functions return one value:

def my_function():
    return 1

def my_function():
    return "Hello"

def my_function():
    return (1+1)/1

e.g.

def my_function():
    return 1

print(my_function())

Many return values

def my_function():
    return 2, "A", 79, 3.1314


print(my_function()) # -> (2, 'A', 79, 3.1314)
abcd = my_function() # -> (2, 'A', 79, 3.1314)
print(abcd) # -> (2, 'A', 79, 3.1314)

a, b, c, d = my_function()

print(a) # -> 2
print(b) # -> A
print(c) # -> 79
print(d) # -> 3.1314

a, b, c = my_function() # ValueError: too many values to unpack (expected 3)

Functions are objects

Functions are objects. Without () at the end, you interact with the function. With the () you use the function:

def my_function():
    return 1

print(my_function)  # -> <function my_function at 0x7f956166a520>
print(my_function())  # -> 1

Interesting, but why do I need to know this? Good question, imaginary person! We can use functions as "parameters" via arguments for other functions:

def my_function_0():
    return 3

def my_function_1():
    return 2


def my_function_2(func):
    return func() ** 2

print(my_function_2(my_function_0))  # -> 9
print(my_function_2(my_function_1))  # -> 4

Default Argument Values

We can define a default value for arguments:

def my_function(a=2, b=7, c=5, d=8):
    return a + b**2 + c**3 + d**4

print(my_function()) # -> 4272

We can (partially) overwrite the default values:

def my_function(a=2, b=7, c=5, d=8):
    return a + b**2 + c**3 + d**4

print(my_function(b=2,a=3)) # -> 4228
print(my_function(3,2)) # -> 4228

In the case of my_function(3,2), the default values are replaced in the order the arguments of the function are defined. a,b,c, and finally d. When using the keyword e.g. d. Then I can overwrite only selected arguments.

Arguments and Keywords