Intro to Programming Tutorial menu

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

python
def greet():
    print("Welcome to Workownix!")
    print("Let's start learning.")

greet()
greet()
Output
Welcome to Workownix!
Let's start learning.
Welcome to Workownix!
Let's start learning.

Example

javascript
function greet() {
  console.log("Welcome to Workownix!");
  console.log("Let's start learning.");
}

greet();
greet();
Output
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

python
def greet(name):
    print("Hello,", name)

greet("Ali")
greet("Ayesha")
Output
Hello, Ali
Hello, Ayesha

Example

javascript
function greet(name) {
  console.log("Hello,", name);
}

greet("Ali");
greet("Ayesha");
Output
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

python
def bill(price, quantity):
    return price * quantity

total = bill(150, 4)
print("Total: Rs", total)
print("Two pizzas: Rs", bill(1200, 2))
Output
Total: Rs 600
Two pizzas: Rs 2400

Example

javascript
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));
Output
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

python
def result(marks):
    if marks >= 50:
        return "Pass"
    return "Fail"

for m in [78, 45, 50]:
    print(m, result(m))
Output
78 Pass
45 Fail
50 Pass

Example

javascript
function result(marks) {
  if (marks >= 50) {
    return "Pass";
  }
  return "Fail";
}

for (let m of [78, 45, 50]) {
  console.log(m, result(m));
}
Output
78 Pass
45 Fail
50 Pass

Quick Comparison

IdeaPythonJavaScript
Define a functiondef name():function name() { }
Call a functionname()name();
Give back a valuereturn valuereturn value;
Body of the functionindented linesinside { }

Exercise

Exercise

What does a function's return statement do?

Learn Intro to Programming with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp