Functions
A function is a named block of code that does one job. You write it once and use it as many times as you like.
Why Use Functions?
You have already used functions: print() in Python and console.log() in JavaScript. Someone else wrote them, and you just call them by name.
Writing your own functions helps you:
- Avoid repeating code: write the steps once, use them everywhere
- Keep code organised: each function has one clear job
- Fix bugs in one place: change the function and every use is fixed
Defining and Calling a Function
Python uses the word def; JavaScript uses the word function.
Example
def greet():
print("Welcome to Workownix!")
print("Let's start learning.")
greet()
greet()Welcome to Workownix! Let's start learning. Welcome to Workownix! Let's start learning.
Example
function greet() {
console.log("Welcome to Workownix!");
console.log("Let's start learning.");
}
greet();
greet();Welcome to Workownix! Let's start learning. Welcome to Workownix! Let's start learning.
Defining a function does not run it. The code inside runs only when you call it by writing its name followed by brackets (). We called it twice, so the message appears twice.
Parameters: Giving a Function Input
A function becomes more useful when you can give it information. The names inside the brackets are called parameters. The values you pass in when calling are called arguments.
Example
def greet(name):
print("Hello,", name)
greet("Ali")
greet("Ayesha")Hello, Ali Hello, Ayesha
Example
function greet(name) {
console.log("Hello,", name);
}
greet("Ali");
greet("Ayesha");Hello, Ali Hello, Ayesha
A function can take more than one parameter. Separate them with commas, for example def bill(price, quantity):.
Returning a Value
Often you want a function to work something out and give back the answer, so you can store or use it. For that we use return.
Example
def bill(price, quantity):
return price * quantity
total = bill(150, 4)
print("Total: Rs", total)
print("Two pizzas: Rs", bill(1200, 2))Total: Rs 600 Two pizzas: Rs 2400
Example
function bill(price, quantity) {
return price * quantity;
}
let total = bill(150, 4);
console.log("Total: Rs", total);
console.log("Two pizzas: Rs", bill(1200, 2));Total: Rs 600 Two pizzas: Rs 2400
When the computer reaches return, the function stops and the value goes back to the place where it was called.
print() and return are not the same. print() only shows a value on the screen. return hands the value back to your program so you can save it in a variable or use it in a calculation.
Putting It All Together
Functions can use conditions and loops inside them. This one decides whether a student has passed:
Example
def result(marks):
if marks >= 50:
return "Pass"
return "Fail"
for m in [78, 45, 50]:
print(m, result(m))78 Pass 45 Fail 50 Pass
Example
function result(marks) {
if (marks >= 50) {
return "Pass";
}
return "Fail";
}
for (let m of [78, 45, 50]) {
console.log(m, result(m));
}78 Pass 45 Fail 50 Pass
Quick Comparison
| Idea | Python | JavaScript |
|---|---|---|
| Define a function | def name(): | function name() { } |
| Call a function | name() | name(); |
| Give back a value | return value | return value; |
| Body of the function | indented lines | inside { } |
Exercise
Exercise
What does a function's return statement do?
