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.

What this concept actually says

  • A function is a named, reusable block of code that takes inputs (parameters) and produces an output (return value)
  • Functions prevent repetition and make code testable, readable, and shareable
  • The difference between defining a function and calling it is fundamental to understanding how programs execute

An analogy your child will recognise

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.

Common misconceptions to watch for

  • Defining a function runs its code — in reality, defining only creates the function; the code runs only when the function is called
  • A function must always return something — in reality, many useful functions perform actions (like writing to a file) and return nothing

Key facts in one breath

  • In Python, functions are defined with the def keyword followed by the function name and parentheses
  • A function without a return statement returns None by default
  • Functions can have default parameter values: def greet(name='Student') works even if no argument is passed
  • Variables created inside a function are local — they do not exist outside it

How Dhee Learning teaches this — the 3-stage question loop

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

Related concepts

Want your child to actually understand this?

Dhee turns this concept into a 15-minute spoken session — asking, listening, and probing — so your child builds the idea themselves.

Frequently asked questions

What is functions — explained for kids? +

A function is reusable code that takes inputs and returns an output. How to write one with def. For Class 7.

What's the most common mistake children make about this concept? +

Defining a function runs its code — in reality, defining only creates the function; the code runs only when the function is called

How does Dhee Learning teach this in a Class 7 session? +

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.