voidex/20-29 Areas/20 Programming/20.01 Python/functions.md

924 B

title tags aliases
functions
python
functions
def
function definition
function syntax
def

[!note] Function Basics
A function in Python is defined using the def keyword, followed by a name, parameters in parentheses, and a colon. The body of the function must be indented.

[!example] Defining and Calling Functions

def add(a, b):
    return a + b
 
print(add(3, 4))  # 7

[!important] Indentation Matters
Python uses significant indentation. Inconsistent spacing will raise an IndentationError.

[!example] Printing vs Returning

def add_two_nums(num_one, num_two):  
    total = num_one + num_two  
    print(total)

add_two_nums(32512, 325235623)  # 325268135

[!info] Related Notes
expressions
docstrings
pythonSyntaxOverview