0% found this document useful (0 votes)
14 views11 pages

Python Functions and Recursion Guide

This document explains functions and recursion in Python, highlighting the definition of functions, parameters, and arguments. It discusses the benefits of using f-strings for readability and performance, and provides an example of recursion through the factorial function. The document also mentions implementing a recursive power function.

Uploaded by

nuray.edilbek
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views11 pages

Python Functions and Recursion Guide

This document explains functions and recursion in Python, highlighting the definition of functions, parameters, and arguments. It discusses the benefits of using f-strings for readability and performance, and provides an example of recursion through the factorial function. The document also mentions implementing a recursive power function.

Uploaded by

nuray.edilbek
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

FUNCTIONS AND

RECURSION
IN PYTHON
WHAT IS A FUNCTION?
 A function is a block of reusable code that
performs a specific task.
 Helps in code reusability, organization,
and readability.
 Defined using the def keyword.
FUNCTION PARAMETERS
AND ARGUMENTS
 Parameters: Variables inside function definition.
 Arguments: Values passed when calling the function.
WHY USE F-STRINGS?
 Readability – f-strings make it easy to embed variables
inside a string.
 Conciseness – It avoids using concatenation (+) or .format().
 Performance – f-strings are faster than .format() and
concatenation.

Alternative Approaches
 Instead of f'Hello, {name}!', you could write:
RETURN STATEMENT
 Functions can return values using the return keyword.
RECURSION
 Recursion is when a function calls itself to solve a smaller
instance of a problem.
 Each recursive call moves closer to a base case to prevent
infinite loops.
EXAMPLE – FACTORIAL USING
RECURSION
 Factorial formula: n! = n × (n-1)
× ... × 1
UNDERSTANDING RECURSIVE CALLS
 How factorial(3) works:
 1. factorial(3) → 3 * factorial(2)
 2. factorial(2) → 2 * factorial(1)
 3. factorial(1) → 1 (Base case)
POWER FUNCTION
 Implement a recursive function to calculate
x^n.
THANK YOU!

07/03/2025
TWO-DIMENSIONAL LISTS

14/03/2025
MIDTERM QUIZ

You might also like