CodingNic

Functions and Reusability

Functions Challenges and Projects

Functions and Reusability 30 min read

Functions Challenges and Projects

Functions Challenges and Projects

Great job finishing this module.

You have learned how to use:

  • functions
  • parameters
  • arguments
  • return values
  • scope
  • keyword arguments
  • lambda functions
  • recursion
  • docstrings

Now it is time to practice everything together.

Quick Challenges

Try to solve these on your own.

Challenge 1: Greeting Function

Build a greeting function.

Steps:

  • Create a function called greet
  • Give it one parameter called name
  • Print:
    Hello and the name
  • Call the function with your name

Expected output:

text
Hello Sara

Challenge 2: Square Function

Build a square function.

Steps:

  • Create a function called square
  • Give it one parameter called number
  • Return the square of the number
  • Print the result of 4

Expected output:

text
16

Challenge 3: Keyword Profile

Build a profile function.

Steps:

  • Create a function with parameters:
    name, age
  • Print both values
  • Call the function using keyword arguments

Expected output:

text
Maya 25

Challenge 4: Countdown Recursion

Build a recursive countdown.

Steps:

  • Create a function called countdown
  • If number is 0, print:
    Go!
  • Otherwise:
    • Print the number
    • Call the function again with number - 1

Expected output for countdown(3):

text
3
2
1
Go!

Mini Projects

These projects combine many skills.

Project 1: Reusable Calculator

Build a calculator using functions.

Steps:

  • Create these functions:
    • add(a, b)
    • subtract(a, b)
    • multiply(a, b)
    • divide(a, b)
  • Each function should return the answer
  • Print results using 10 and 2

Expected output:

text
12
8
20
5.0

Project 2: Student Result Checker

Build a student result app.

Steps:

  • Create a function called result
  • Give it parameters:
    name, score

Rules:

  • If score is 80 or more, return Excellent
  • If score is 50 or more, return Pass
  • Otherwise, return Fail

Then print:

Name: Result

Expected output:

text
Sara: Pass

Project 3: Text Formatter

Build a text tool.

Steps:

  • Create a function called format_text
  • Give it one parameter called text
  • Return:
    • uppercase text
    • lowercase text
    • title case text

Print all results.

Expected output for python course:

text
PYTHON COURSE
python course
Python Course

Project 4: Login Function

Build a login checker.

Steps:

  • Store correct username as admin
  • Store correct password as python123
  • Create a function called login
  • Give it parameters:
    username, password

Rules:

  • If both values are correct, return:
    Login successful
  • Otherwise, return:
    Invalid login

Print the result.

Expected output:

text
Login successful

Final Challenge

Build a mini banking app.

Steps:

  • Create a function called deposit(balance, amount)
  • Return new balance after adding amount
  • Create a function called withdraw(balance, amount)

Rules:

  • If amount is greater than balance, return:
    Not enough money
  • Otherwise, return new balance
  • Add docstrings to both functions
  • Test your functions with example values

Expected output:

text
150
120
Not enough money

Real World Use Case

Real programs are built with functions. Websites, games, apps, and automation tools all use reusable functions to stay organized.

Quiz

  1. What does return do?
  2. What is a parameter?
  3. Why are functions useful?
  4. What is recursion?

Assignment

Choose one mini project and improve it by adding more features or cleaner output.

Summary

You completed the Functions module and practiced building reusable Python programs with real function skills.