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

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.

Note
Example Prompts
  • Show how to define and call a simple function in Python.
  • Give examples of a function with one and two parameters. Show how to call it with different arguments.
  • How do you define a function with a default argument in Python? Show an example with and without the argument.
  • Show how a function returns a result in Python. Include an example where the return value is stored and printed.
  • What is variable scope in Python functions? Show a code example that defines a variable inside a function and explain what happens outside of it.

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.

Try It Yourself

  1. Define a function greet() that prints "Welcome!";
  2. Define a function multiply(a, b) that returns the product;
  3. Add a third function compare(a, b) that returns the larger number;
  4. Try calling greet() and printing the result of multiply(2, 5) and compare(3, 10).
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 5

bookUser-defined Functions

Sveip for å vise menyen

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.

Note
Example Prompts
  • Show how to define and call a simple function in Python.
  • Give examples of a function with one and two parameters. Show how to call it with different arguments.
  • How do you define a function with a default argument in Python? Show an example with and without the argument.
  • Show how a function returns a result in Python. Include an example where the return value is stored and printed.
  • What is variable scope in Python functions? Show a code example that defines a variable inside a function and explain what happens outside of it.

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.

Try It Yourself

  1. Define a function greet() that prints "Welcome!";
  2. Define a function multiply(a, b) that returns the product;
  3. Add a third function compare(a, b) that returns the larger number;
  4. Try calling greet() and printing the result of multiply(2, 5) and compare(3, 10).
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 2
some-alt