KwickAcademy Python · 7 min · free
User-defined Functions: Defining, Calling and Returning Values
Learn Python user-defined functions for CBSE: why functions, def, calling, parameters, return, multiple values, flow of execution. A function is a named block of code that does one job; you define it with def and run it with a call.
Follows the syllabus of: CBSE Class 11 Computer Science (083), CBSE Class 11 Computer Science Essentials (083), CBSE Class 11 Informatics Practices (065), CBSE Class 12 Computer Science (083)
On screen in this lesson
Why do we need functions?
| A function is a named block of code for one job |
| Reuse: write it once, call it many times |
| Modularity: split a big program into small parts |
| Easier to read, test and fix |
Three kinds of functions
| Kind | Comes from | Example |
|---|---|---|
| Built-in | Always ready | print(), len() |
| Module | Needs import | math.sqrt() |
| User-defined | You write it | bill() |
Rules for writing a function
| Start with def, then the name and ( ) |
| Parameters go inside the brackets |
| End the def line with a colon |
| Indent the body, usually 4 spaces |
| No colon: SyntaxError; no indent: IndentationError |
Flow of execution: bill(4)
| Step | Line that runs | What happens |
|---|---|---|
| 1 | def bill(...) | Saved, not run |
| 2 | cost = bill(4) | Jump into bill |
| 3 | total = 4 * 15 | total is 60 |
| 4 | return total | Jump back with 60 |
| 5 | print(cost) | Prints 60 |
Quick recap
| Functions give reuse and modularity |
| Built-in, module and user-defined functions |
| def defines; a call with ( ) runs the body |
| return sends values back and ends the function |
| Trace calls step by step to predict output |
Quick answers
What does a function return if it has no return statement?
None.
Does defining a function run it?
No. Python only saves the def. A call runs the body.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
What is reuse?43 sec
Why does square root need import?41 sec
Can a Python function return two values?44 sec
Does the def line run the function?43 sec
What does si(10000, 5, 2) return?39 secThe full lesson, in text
Hello students, welcome to Kwickprep. Suppose you must work out the canteen bill for fifty students. Will you type the same lines fifty times? No. Today we will learn to write our own functions, call them, and get answers back from them.
First, a new term. A function is a block of code with a name, and it does one job. Reuse means we write the code once and use it again and again. Modularity means we break a big program into small parts, like chapters in a textbook. Small parts are easier to read, to test and to fix.
Python functions come in three kinds. Built-in functions are always ready to use, like print and len. Module functions live inside a module, which is a separate file of ready code, so we must import it first. User-defined functions are the ones we write ourselves, and they are today's topic.
Here are the first two kinds in action. Len is a built-in function, and it counts the six letters in Mumbai. The square root function belongs to the math module. So the first line says import math. Then math dot square root of forty nine gives seven point zero.
Now let us write our own function. The keyword def means define. Then comes the name greet, round brackets, and a colon. The indented line below is the body of the function. Defining a function does not run it. Writing greet with brackets is a call, and a call runs the body. We call it twice, so Namaste prints twice.
Here are the rules for writing a function. Start with def, then the function name and round brackets. A parameter is a variable inside the brackets that receives a value from the call. The def line must end with a colon. The body must be indented, which means pushed right, usually by four spaces. Forget the colon and you get a syntax error, and forget the indent and you get an indentation error.
Now a function that gives an answer back. Samosas is the parameter. A samosa costs fifteen rupees, and the star sign is the multiply operator. The return statement sends a value back to the line that called the function. We call bill of four, so samosas becomes four and total becomes sixty. Return sends sixty back, it is stored in cost, and sixty is printed.
Students often mix up print and return. This function prints ten, but it returns nothing. A function without a return gives back a special value called None. So the variable ans stores None, and the second print shows None. Print shows a value on the screen. Return hands the value back so we can store and use it.
A Python function can return more than one value. Write the values after return, separated by commas. Python packs them into a tuple, which is a fixed group of values. Here the marks are eighty, seventy and ninety. The total is two hundred forty, and the slash operator divides it by three to give eighty point zero. On the calling line, tot gets the total and avg gets the average.
Flow of execution means the order in which Python runs the lines. Let us trace the samosa bill program. In step one, Python reads the def line and saves the function, but runs nothing inside it. In step two, the call bill of four makes Python jump into the function. In step three, total becomes four times fifteen, which is sixty. In step four, return jumps back to the calling line, carrying sixty. In step five, cost holds sixty and it is printed.
Exams often ask you to predict output such as this. The def is only saved, so the first print shows A. Next, print of tea calls the function. Inside, B is printed and C is returned. Then the outer print shows C. Finally D prints. So the order is A, B, C, D.
Return also stops the function at once. Here, greater than or equal to checks if marks are thirty three or more. If yes, the function returns Pass and the last line never runs. Pause and predict. What prints for exactly thirty three, and for thirty two? Thirty three gives Pass, because greater than or equal to includes thirty three. Thirty two gives Fail.
Now a program using a function. Simple interest is principal times rate times time, divided by one hundred. Here amt is the principal amount, rate is the rate, and yrs is the time in years. Ten thousand rupees at five percent for two years gives one thousand. We reuse the same function for five thousand rupees at six percent for three years, which gives nine hundred.
One more program. The percent sign is the modulus operator, and it gives the remainder after division. Double equals compares two values, while a single equals stores a value. If the remainder after dividing by two is zero, the number is even. Fourteen gives True, and seven gives False.
Some exams ask you to write functions in pseudocode. The function line names the parameter type and the type it returns. Return sends back the answer, and end function closes it. A block that returns nothing is called a procedure in pseudocode, and it is run with the word call.
Let us revise what we learned today. Functions let us reuse code and split programs into small parts. There are built-in, module and user-defined functions. Def defines a function, and a call with brackets runs it. Return sends one or more values back and ends the function at once. To predict output, trace every call step by step, just as we did.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 10 Information Technology (402) | Electronic Spreadsheet (Advanced) using LibreOffice Calc |
| CBSE Class 11 Computer Science (083) | Computational Thinking and Programming - I |
| CBSE Class 11 Computer Science Essentials (083) | Computational Thinking and Programming - I |
| CBSE Class 12 Computer Science (083) | Computational Thinking and Programming – 2 |
| CBSE Class 11 Informatics Practices (065) | Introduction to Python |
| GSEB Std 10 Computer Studies | Spreadsheets with LibreOffice Calc |
| Cambridge IGCSE Grade 9 Computer Science (0478) | 8. Programming |
| Programming All levels Python | Functions |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

