Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn What Is Function? | What Is a Function in Python?
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Python Functions Tutorial

bookWhat Is Function?

Note
Definition

In Python, a function is a named block of reusable code that performs a specific task. Functions act like LEGO pieces: small, reusable building blocks you can use throughout your program.

Using functions helps avoid rewriting code, reduces mistakes, and breaks large tasks into smaller steps. Each function handles one action, making programs clearer and easier to maintain. In short, functions let you reuse logic and keep your code organized.

Defining a Function

Define the simplest function in Python.

  1. Use the def keyword;
  2. Write the function name using snake_case;
  3. Add (): β€” parameters go inside the parentheses;
  4. Write the function body on an indented line;
  5. Optionally specify a return value;
  6. Call the function using its name.

Create a simple function that prints 'Hello, world!' and call it.

123456789
# Specify the function name # The function doesn't require any parameters, so we simply use `():` def print_hello(): # Function body: we have to use indentation when defining it. print('Hello, world!') # The function doesn't return anything, so we don't need to write additional code # Call the function print_hello()
copy

The print() inside print_hello() is a built-in function. A function may perform an action without returning a value. Since print_hello() only prints a message and doesn’t provide output for further use, it has no return value.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookWhat Is Function?

Swipe to show menu

Note
Definition

In Python, a function is a named block of reusable code that performs a specific task. Functions act like LEGO pieces: small, reusable building blocks you can use throughout your program.

Using functions helps avoid rewriting code, reduces mistakes, and breaks large tasks into smaller steps. Each function handles one action, making programs clearer and easier to maintain. In short, functions let you reuse logic and keep your code organized.

Defining a Function

Define the simplest function in Python.

  1. Use the def keyword;
  2. Write the function name using snake_case;
  3. Add (): β€” parameters go inside the parentheses;
  4. Write the function body on an indented line;
  5. Optionally specify a return value;
  6. Call the function using its name.

Create a simple function that prints 'Hello, world!' and call it.

123456789
# Specify the function name # The function doesn't require any parameters, so we simply use `():` def print_hello(): # Function body: we have to use indentation when defining it. print('Hello, world!') # The function doesn't return anything, so we don't need to write additional code # Call the function print_hello()
copy

The print() inside print_hello() is a built-in function. A function may perform an action without returning a value. Since print_hello() only prints a message and doesn’t provide output for further use, it has no return value.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 1
some-alt