Title: Python Functions: def, Arguments, Return, Examples | TheCodeForge
Open Graph Title: Python Functions: def, Arguments, Return, Examples | TheCodeForge
Description: Python functions explained: def, arguments, defaults, *args and **kwargs, return values and scope — plus the mutable default argument trap.
Open Graph Description: Python functions explained: def, arguments, defaults, *args and **kwargs, return values and scope — plus the mutable default argument trap.
Opengraph URL: https://thecodeforge.io/python/functions-python/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/python/functions-python/"
},
"headline": "Python Functions - Silent None Return Crashes",
"description": "Python functions explained: def, arguments, defaults, *args and **kwargs, return values and scope — plus the mutable default argument trap.",
"keywords": "Python Functions - Silent None Return Crashes, Python, Beginner, Functions, Python functions explained: def, arguments, defaults, *args and **kwargs, return values and scope — plus the mutable default argument trap.",
"proficiencyLevel": "Beginner",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-05T00:00:00+00:00",
"dateModified": "2026-09-23T00:00:00+00:00",
"url": "https://thecodeforge.io/python/functions-python/",
"inLanguage": "en",
"educationalLevel": "Beginner",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "Python", "item": "https://thecodeforge.io/python/"},
{"@type": "ListItem", "position": 3, "name": "Python Functions - Silent None Return Crashes", "item": "https://thecodeforge.io/python/functions-python/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is a function in Python and why do we use it?",
"acceptedAnswer": {
"@type": "Answer",
"text": "A function is a named, reusable block of code that performs a specific task. We use functions to avoid repeating the same code in multiple places — the DRY principle — to break large problems into smaller, testable pieces, and to make code easier to maintain. You define it once with `def` and call it as many times as needed. If the logic changes, you update it in one place and every caller automatically gets the new behaviour."
}
},
{
"@type": "Question",
"name": "What is the difference between return and print in a Python function?",
"acceptedAnswer": {
"@type": "Answer",
"text": "`print()` displays a value on screen and then it's gone — you can't store or reuse it. `return` sends the value back to whatever code called the function, where it can be stored in a variable, used in a calculation, or passed to the next function in a pipeline. If you ever find yourself storing the result of a function — `result = my_func()` — and getting None, the function is almost certainly using print instead of return. The fix is always to add the return statement."
}
},
{
"@type": "Question",
"name": "Can a Python function return more than one value?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes — Python lets you return multiple values by separating them with commas: `return width, height`. Python automatically wraps them in a tuple, and on the calling side you can unpack them cleanly: `w, h = get_dimensions()`. This is idiomatic Python and appears constantly in professional codebases. You can also capture the whole tuple as a single variable and access elements by index if you prefer."
}
},
{
"@type": "Question",
"name": "What happens if I forget to call a function — just write its name without parentheses?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Python won't raise an error — it will give you the function object itself rather than executing it and returning the result. This is a common beginner mistake: writing `result = my_function` instead of `result = my_function()`. The first stores a reference to the function; the second actually calls it and stores the return value. Always include parentheses when you intend to call a function. Type checkers like mypy can catch this mistake before it reaches production."
}
},
{
"@type": "Question",
"name": "What is the LEGB rule in Python variable scope?",
"acceptedAnswer": {
"@type": "Answer",
"text": "LEGB stands for Local, Enclosing, Global, Built-in — the order Python searches for a variable name when it encounters one. First it checks Local (inside the current function), then Enclosing (outer functions, for nested functions), then Global (module level), then Built-in (Python's built-in names like print, len, range). Python stops at the first match. This explains why a local variable shadows a global one with the same name, why nested functions can access outer function variables without any special keyword, and why you can always call print() without importing it."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large