39 lines
924 B
Markdown
39 lines
924 B
Markdown
---
|
|
title: functions
|
|
tags:
|
|
- python
|
|
- functions
|
|
- def
|
|
aliases:
|
|
- 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
|
|
> > ```python
|
|
> > 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
|
|
> > ```python
|
|
> > 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]]
|