# Functions

## Write and call a function

To avoid rewriting the same code in multiple places, we use functions. A function is defined with `def()` keyword.

Python uses indentation to identify blocks of code. Code within the same block should be indented at the same level.

``````def function_name(input1, input2):
# do something with the inputs
return output
function_name()
``````

For example

``````def sum(x,y):
return x + y

print(sum(2,4)) # Output: 6
``````

## Common function errors

common function errors include:

• Forgetting closing parenthesis
• Forgetting commas between each argument

## Scope of variables

### Local variables

Variable defined inside a function is a local variable. It cannot be used outside of the scope of the function.

### Global Variables

Variable defined outside of a function is a global variable and it can be accessed inside the body of a function.

## Returning Multiple Values

Python functions are able to return multiple values using one return statement. All values that should be returned are listed after the return keyword and are separated by commas.

``````def calculation(x, y):
subtraction = x - y
return addition, subtraction    # Return all two values
``````

## Lambda Functions

A lambda function is a one-line shorthand for function. It allows us to efficiently run an expression and produce an output for a specific task

Benefits:

• Lambda functions are great when you need to use a function once.
• Save the work of defining a function

Syntax

``````lambda arguments : expression
``````

Example 1 Create a lambda function named contains_a that takes an input word and returns True if the input contains the letter ‘a’. Otherwise, return False.

``````contains_a = lambda word: "a" in word
``````
• The function is stored in a variable called contains_a
• Declare lambda function with an input called word (lambda word:)
• Return True if this statement is true
• Return False if this statement is not true

Example 2 The function will return your input minus 1 if your input is positive or 0, and otherwise will return your input plus 1.

``````add_subtract = lambda num: num - 1 if num >= 0 else num + 1
``````

Example 3 Create a lambda function named even_odd that takes an integer named num. If num is even, return “even”. If num is odd, return “odd”

``````even_odd = lambda num: "even" if num % 2 == 0 else "odd"
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.6370588,"math_prob":0.92788523,"size":1719,"snap":"2021-43-2021-49","text_gpt3_token_len":394,"char_repetition_ratio":0.15276968,"word_repetition_ratio":0.0067340066,"special_character_ratio":0.23443863,"punctuation_ratio":0.10703364,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99211633,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-26T23:26:51Z\",\"WARC-Record-ID\":\"<urn:uuid:84f89ed2-506e-46fb-ae55-70d576e82d10>\",\"Content-Length\":\"34649\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c77e35cd-08a6-401a-a9d0-904b42d0a177>\",\"WARC-Concurrent-To\":\"<urn:uuid:d86d073b-4712-45d6-b7fa-9cfbc6ed9684>\",\"WARC-IP-Address\":\"34.194.149.67\",\"WARC-Target-URI\":\"https://www.devhandbook.com/python/functions/\",\"WARC-Payload-Digest\":\"sha1:ZGSFEXKQ235QV3RK72FL3XEH5LBVQTAS\",\"WARC-Block-Digest\":\"sha1:MFS4EA7IOESN45HDVR5W4MYVVH5FM7RI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587963.12_warc_CC-MAIN-20211026231833-20211027021833-00097.warc.gz\"}"}