Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn User-defined Functions | Functions & Modularity
Introduction to Python with Cursor

bookUser-defined Functions

In Python, a function is a named block of code for a specific task. You've already used built-ins like print() or len().

Defining your own functions helps avoid repetition, organize logic, and make programs clearer and easier to maintain.

Defining a Function

To define a function, you use the def keyword, followed by a name, parentheses, and a colon. The code that runs is written on the next line with indentation.

Parameters and Arguments

You can pass values into a function by listing parameters in the parentheses. These values are used inside the function when it runs.

Default Parameters

You can also give parameters default values. That means the function still works even if no argument is passed in.

Returning Values

Functions can give back a result using the return keyword. The returned value can be stored in a variable or used directly in expressions.

Scope and Variables

Variables that are created inside a function exist only within that function. They're not visible or accessible from the outside.

Summary

  • Use def to define a function;
  • Use parameters to pass input;
  • Use return to give a result back;
  • Variables inside a function are local;
  • Functions help structure and reuse code.
question mark

What keyword is used to define a function in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

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

Suggested prompts:

Can you show more examples of defining functions in Python?

What are some common mistakes when defining functions?

How do I choose a good name for my function?

Awesome!

Completion rate improved to 5

bookUser-defined Functions

Swipe to show menu

In Python, a function is a named block of code for a specific task. You've already used built-ins like print() or len().

Defining your own functions helps avoid repetition, organize logic, and make programs clearer and easier to maintain.

Defining a Function

To define a function, you use the def keyword, followed by a name, parentheses, and a colon. The code that runs is written on the next line with indentation.

Parameters and Arguments

You can pass values into a function by listing parameters in the parentheses. These values are used inside the function when it runs.

Default Parameters

You can also give parameters default values. That means the function still works even if no argument is passed in.

Returning Values

Functions can give back a result using the return keyword. The returned value can be stored in a variable or used directly in expressions.

Scope and Variables

Variables that are created inside a function exist only within that function. They're not visible or accessible from the outside.

Summary

  • Use def to define a function;
  • Use parameters to pass input;
  • Use return to give a result back;
  • Variables inside a function are local;
  • Functions help structure and reuse code.
question mark

What keyword is used to define a function in Python?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 2
some-alt