Lesson 16
Introduction to Functions
Package logic into reusable blocks
1What Are Functions?✨
Functions are reusable blocks of code that perform a specific task. They help organize your code and make it easier to manage. Think of functions as mini-programs within your program!
2Why Use Functions?🎯
- ✓ Reduce code repetition
- ✓ Simplify complex problems
- ✓ Make your code more readable
- ✓ Easily update and maintain code
3Creating a Function💡
To create a function, use the `def` keyword followed by the function name and parentheses. Here’s how you can define a simple function:
def greet(name):
print("Hello, " + name + "!")This function takes a name as input and prints a greeting.
4Calling a Function🚀
Once you define a function, you can use (or "call") it whenever needed. Here’s how to call the `greet` function:greet("Alice")
greet("Bob")5This will output:🔍
- Hello, Alice!
- Hello, Bob!
6Parameters and Return Values⭐
Functions can take parameters and return values. Here’s an example of a function that adds two numbers and returns the result:
def add_numbers(a, b):
return a + bresult = add_numbers(5, 3)
print(result)7This will output:🧩
8
8Putting It All Together🎈
Now, let’s create a function that calculates the area of a rectangle. You will use parameters for length and width and return the area:
def calculate_area(length, width):
return length * widtharea = calculate_area(5, 4)
print("The area is:", area)9This will output:✨
The area is: 20
🎯 Practice Challenges
Master Introduction to Functions by completing all 3 difficulty levels
Introduction to Functions - Easy Challenge
Start with the basics. Perfect for beginners!
Introduction to Functions - Medium Challenge
Get more challenging. Build your confidence!
Introduction to Functions - Hard Challenge
Master this concept. Prove your skills!
🏆 Study a lesson for 2+ minutes to earn +10 TalentJR Points a day. Badges, certificates & prizes →
Ready to Master Introduction to Functions? 🚀
Complete the challenges, take the quiz and keep your learning streak going!
Keep learning
- ← Previous lessonLesson 15: Loop Control PatternsSolve classic loop-based puzzles
- Next lesson →Lesson 17: Number Guessing GameBuild a complete guessing game
- Grade 7 coursePython Functions & DataPython coding course for kids.
- Student ToolsFree calculators for studentsPercentage, fraction, ratio and marks calculators with steps.
- CBSEClass 6 CBSE & NCERT study materialMaths, Science and Computer chapters with solutions.
- Future SkillsExplore future skillsAI, robotics and new-age skills explained for students.