2 of 1 ·Functions

Lesson 2 · Python Foundation
Reusable blocks of code.
Lists & Functions
A list holds several values. A function gives a small job a name so you can use it again. Practise one list and one short function separately before combining them.
Click to zoomA function is a named set of instructions. Define it once, then call its name to run it.
def say_hello():
print("Hello!")
say_hello()
say_hello()
The indented line is the job. Each say_hello() at the bottom runs that job once. Predict how many greetings appear, then run it.
हिंदी में: Function छोटे काम को नाम देता है। नीचे उसका नाम लिखकर काम चलाते हैं। दो बार चलाएँगे तो दो greetings दिखेंगी।
Click to zoomA parameter lets a function receive a value. return sends a result back to the program.
def double(number):
return number * 2
answer = double(3)
print(answer)
Trace it: 3 goes in, 3 × 2 is 6, 6 comes back, and print displays 6. Change 3 to 4 and predict the result.
print displays a value. return gives a value back so other code can use it. Without an explicit return, a Python function returns None.
हिंदी में: Parameter में value आती है। Return नतीजा program को वापस देता है। Print उसे screen पर दिखाता है।
9 more parts in this lesson
Walkthroughs, code labs, mini-games and the checkpoint quiz unlock when you buy the course.