Class 7 · CBSE AI · Strand B — Python for AI
Python functions for beginners — def explained for Class 7
A function is reusable code that takes inputs and returns an output. How to write one with def. For Class 7.
Class 7 · CBSE AI · Strand B — Python for AI
A function is reusable code that takes inputs and returns an output. How to write one with def. For Class 7.
Idli mould
A function is like an idli mould. You define the mould once. Every time you want idlis, you pour in batter (the argument) and get perfectly shaped idlis out (the return value). You do not carve a new mould each time you are hungry.
Post office stamp
A rubber stamp at the post office is defined once — the design is fixed. Every time a parcel arrives (argument), you stamp it and get a consistent result (return value). Functions work exactly this way: define once, apply many times.
Every Dhee Learning session for this concept follows three stages. We share the questions Dhee actually asks, so you can hear what a session sounds like.
Stage 1 — Surface
You have written code to calculate a student's grade category three times in three different places in your notebook. What problem does this cause, and what is the obvious fix?
Rote answer
"A function lets you reuse code"
Understood
"If the grading logic changes, you would have to find and fix it in three places and might miss one — a function means you fix it once in the definition and every call automatically uses the new version"
Stage 2 — Reasoning
What is the difference between a parameter and an argument? And why does a function that returns a value give you more power than one that just prints the result?
Follow-up Dhee may use: Think of a real-world machine: what is the difference between a machine that shows you the result on a screen versus one that hands the result to the next machine on the production line?
Stage 3 — Application
Write a function called classify_score that takes a number and returns 'Distinction', 'Pass', or 'Needs Support'. Then use it inside a loop over a list of scores. Why is this better than putting the if/elif logic directly inside the loop?
Misconception Dhee watches for: Using print() inside the function instead of return, then being confused why the result cannot be stored in a variable
Dhee turns this concept into a 15-minute spoken session — asking, listening, and probing — so your child builds the idea themselves.
A function is reusable code that takes inputs and returns an output. How to write one with def. For Class 7.
Defining a function runs its code — in reality, defining only creates the function; the code runs only when the function is called
Dhee opens with a question — for example: "You have written code to calculate a student's grade category three times in three different places in your notebook. What problem does this cause, and what is the obvious fix?" — listens to your child's answer, then probes the reasoning behind it. The session ends when the child can apply the idea to a brand-new situation, not just recall it.