Defining and Calling

A function is a named block of instructions you can run whenever you need it. You define it once, then call it as many times as you like.

Without functions, every program is one long scroll of repeated logic. With them, you build a vocabulary of named operations, and your programs start reading like sentences.

Defining a function

PYTHON

def greet():
    print("Welcome to Sankofa Code")

greet()

JAVASCRIPT

function greet() {
  console.log("Welcome to Sankofa Code");
}

greet(); 
Compare → Python's keyword is def, followed by a colon and an indented body. JavaScript's is function, with the body in braces. Both need the empty parentheses in the definition AND in the call, greet() with parentheses RUNS it; greet without them just refers to it.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Write your own function that prints a two-line welcome for your community, then call it twice.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Parameters and Arguments →
← Back