๐ฆ Day 2: Writing Functions with Copilot
๐ฏ Goal:
Learn how to write functions efficiently with GitHub Copilot by giving it smart hints through comments and function names.
๐ง Concept:
GitHub Copilot predicts what you're trying to do when you:
- Write clear comments
- Start a function definition
- Follow proper coding structure
๐ง Step-by-Step Practice (Language: Python — but you can try others too)
✅ Example 1: Use a descriptive comment
# Check if a number is even
➡ Copilot suggests:
def is_even(n):
return n % 2 == 0
✅ Example 2: Use a function name as a clue
def reverse_string(s):
➡ Copilot may suggest:
return s[::-1]
๐ Practice Set – Try these prompts
Type each comment or function name and observe what Copilot suggests. Accept (Tab) or cycle suggestions (Alt + ] and Alt + [):
๐น Basic Math Functions
# Find the factorial of a number
# Return the sum of a list
# Find the largest number in a list
๐น String Functions
# Count vowels in a string
# Check if a string is a palindrome
# Replace spaces with underscores in a sentence
๐น List/Array Operations
# Remove duplicates from a list
# Sort a list of dictionaries by key 'age'
๐น Date/Time Examples
# Get the current date and time
# Check if a given year is a leap year
๐ก Tips for Better Suggestions
| ✅ Do This | ❌ Avoid This |
|---|---|
| Use specific comments | Write vague comments |
| Name functions meaningfully | Use unclear names like func1() |
| Use small problems | Ask for huge complex apps |
๐งช Mini Challenge
Write this as a comment and let Copilot help:
# Write a function to calculate grade from marks (90+ = A, 80+ = B, etc.)
Then try:
# Check if a number is prime
Modify the logic and observe how Copilot adapts to your changes.
๐ Summary of Day 2
- ๐งญ Use comments or function names to guide Copilot
- ๐ Try different ways to phrase your intention
- ๐งช Practice simple utilities, not entire applications yet
- ✅ Review & test the code Copilot writes — don’t assume it's perfect
Tags:
GitHub Copilot