Cherreads

Chapter 18 - Chapter 18 — Building Functions

The computer screen flickered again.

The cursor blinked patiently.

_

Pran leaned forward in his chair.

The robot stood quietly beside the system messages.

[^_^]

/| |\

/ \

A new message appeared.

PYTHON MODULE PROGRESS: 70%

NEXT MODULE: TOOL CREATION

Pran smiled.

"Tool creation… I remember this from C."

In the earlier system modules, he had learned about functions. Functions allowed programmers to create their own commands.

Instead of repeating the same code again and again, programmers could write the instructions once and reuse them whenever they wanted.

The computer displayed another message.

FUNCTIONS ALLOW PROGRAMMERS TO CREATE REUSABLE CODE

Pran nodded.

"That makes sense."

The computer then showed the simplest Python function.

def greet():

print("Hello!")

Pran read the first word carefully.

def

The robot displayed an explanation.

DEF MEANS DEFINE A FUNCTION

So this line:

def greet():

means:

Create a function named greet.

The code inside the function runs whenever the function is called.

The computer showed the next step.

def greet():

print("Hello!")

greet()

Pran typed the program and ran it.

The output appeared.

Hello!

Pran nodded.

"So the function runs when we call it."

Exactly.

Functions are like tools in a toolbox.

You create them once, then use them whenever you need them.

The computer displayed another example.

def greet():

print("Hello!")

print("Welcome to Python!")

greet()

Pran ran the program.

The output appeared.

Hello!

Welcome to Python!

The robot clapped happily.

[^O^]

/| |\

/ \

Another system message appeared.

FUNCTION ACTIVATION VERIFIED

But functions can do even more.

Sometimes functions need information from the user.

The computer displayed another program.

def greet(name):

print("Hello", name)

greet("Alex")

Pran read it carefully.

Inside the parentheses was a new word.

name

That was called a parameter.

Parameters allow functions to receive information.

When the program runs:

greet("Alex")

the value Alex is passed to the function.

The function then prints:

Hello Alex

Pran ran the program.

The output appeared exactly as expected.

Hello Alex

He smiled.

"So functions can receive information."

Exactly.

But functions can also send information back.

The computer showed another example.

def add(a, b):

return a + b

Pran saw a new keyword.

return

The robot displayed a message.

RETURN SENDS A VALUE BACK FROM A FUNCTION

So this function adds two numbers and returns the result.

The full program looked like this.

def add(a, b):

return a + b

result = add(5, 3)

print(result)

Pran ran the program.

The output appeared.

8

Pran leaned back.

"That's really useful."

Functions help organize programs.

Without functions, large programs would become messy and difficult to understand.

But with functions, programmers can break big problems into smaller pieces.

The computer screen flickered again.

Another system message appeared.

TOOL CREATION MODULE VERIFIED

Then another line appeared.

PYTHON MODULE PROGRESS: 80%

Pran crossed his arms.

"Only twenty percent left."

The robot nodded proudly.

[^_^]

/| |\

/ \

The computer displayed another message.

NEXT MODULE: DATA PROCESSING

Pran tilted his head.

"Data processing?"

The robot displayed another concept.

LOOPS + LISTS

Pran remembered both of those ideas.

Loops repeat actions.

Lists store multiple values.

But when combined, they become very powerful.

Programs can process large amounts of data quickly.

The cursor blinked again.

_

Waiting.

Ready for the next lesson.

Pran placed his fingers on the keyboard again.

"Alright," he said quietly.

"Let's process some data."

Next Chapter

Chapter 19 — Processing Data

You will learn:

looping through lists

combining for loops with lists

simple data processing programs

More Chapters