Menu
Coddy logo textTech

Declare a Function

Part of the Fundamentals section of Coddy's Python journey — lesson 43 of 77.

A function is a sequence of code that has a name. The purpose of a function is to reuse a piece of code multiple times.

For example, take a look at this code:

print("Welcome to Coddy")
print("New session...")
print("Welcome to Coddy")
print("Another session...")
print("Welcome to Coddy")

We use the same code print("Welcome to Coddy") over and over again. Another issue with this code is that if we wanted to change the message: Welcome to Coddy to something different, like "Welcome aboard" it would have to change 3 different lines of code. To solve this issue, we will use functions.

To declare a function, we use the following syntax:

def function_name():
	code

For our example, we will create a function named greet and it will look like this:

def greet():
	print("Welcome to Coddy")

To use/call/execute the function, we write greet():

greet()
print("New session...")
greet()
print("Another session...")
greet()

This will result in the same output as above.

Important! The function code must come before it's call

challenge icon

Challenge

Easy

Write a program that gets one input, a number n. Create a named print_large_number function that prints the number 50005000. Then, call that function n times using a for loop, so the number is printed n times in total.

Note! In your code, write the function definition before the loop that calls it.

Cheat sheet

Functions in Python:

# Function declaration
def function_name():
    # Function body (indented)
    code_here

# Function call
function_name()

Try it yourself

# Declare the function print_large_number below


n = int(input())
for i in range(n):
    # Call the function here
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals