Hur man ringer in en funktion Python (Exempel)
โก Smart sammanfattning
Fungerar i Python are reusable blocks of code that run when called, defined with the def statement. They accept arguments, return values, and rely on consistent indentation to group the statements that belong to each function.

Vad รคr funktion i Python?
A Funktion i Python is a piece of code which runs when it is referenced. It is used to utilize the code in more than one place in a program. It is also called a method or a procedure. Python provides many inbuilt functions like print(), input(), compile(), and exec(), but it also gives you the freedom to create your own functions.
Hur man definierar och anropar en funktion Python
En funktion i Python is defined by the โdefโ statement followed by the function name and parentheses ( () ).
Exempel:
Let us define a function by using the command โdef func1():โ and call the function. The output of the function will be โI am learning Python fungera".
The function call func1() invokes our def func1(): and prints the command โI am learning Python function None.โ
There is a set of rules in Python programmering att definiera en funktion.
- Any arguments or input parameters should be placed within these parentheses.
- The function first statement can be an optional statement โ the docstring or documentation string of the function.
- The code within every function starts with a colon (:) and should be indented (space).
- The statement return (expression) exits a function, optionally passing back a value to the caller. A return statement with no arguments is the same as return None.
Betydelsen av indrag (mellanslag) i Python
Before we get familiar with Python funktioner รคr det viktigt att vi fรถrstรฅr indragsregeln att deklarera Python functions, and these rules are applicable to other elements of Python as well, such as declaring conditions, loops, or variables.
Python follows a particular style of indentation to define the code. Since Python functions do not have any explicit begin or end like curly braces to indicate the start and stop of the function, they have to rely on this indentation. Here we take a simple example with the โprintโ command. When we write the โprintโ function right below the def func1(): line, it will show an โindentation error: expected an indented blockโ.
Now, when you add the indent (space) in front of the โprintโ function, it should print as expected.
At least one indent is enough to make your code work successfully. But as a best practice, it is advisable to leave about 3โ4 spaces of indent to call your function.
It is also necessary that while declaring indentation, you maintain the same indent for the rest of your code. For example, in the below screenshot, when we call another statement โstill in func1โ and it is not declared right below the first print statement, it will show an indentation error: โunindent does not match any other indentation level.โ
Now, when we apply the same indentation for both the statements and align them on the same line, it gives the expected output.
Hur fungerar avkastningsvรคrdet?
The return command in Python specifies what value to give back to the caller of the function. Let us understand this with the following example.
Steg 1) Funktionen returnerar ingenting
Here we see when a function does not โreturnโ. For example, we want the square of 4, and it should give the answer โ16โ when the code is executed. It gives that when we simply use the โprint x*xโ code, but when you call the function โprint squareโ it gives โNoneโ as an output. This is because when you call the function, recursion does not happen and it falls off the end of the function. Python returns โNoneโ for falling off the end of the function.
Step 2) Replace the print command with an assignment command
To make this clearer, we replace the print command with an assignment command. Let us check the output.
When you run the command โprint square (4)โ, it actually returns the value of the object. Since we do not have any specific function to run over here, it returns โNoneโ.
Step 3) Use the โreturnโ function and execute the code
Now, here we will see how to retrieve the output using the โreturnโ command. When you use the โreturnโ function and execute the code, it will give the output โ16.โ
Steg 4) Kรถr kommandot "print square"
Fungerar i Python รคr i sig ett objekt, och ett objekt har ett visst vรคrde. Vi fรฅr hรคr se hur Python treats an object. When you run the command โprint squareโ, it returns the value of the object. Since we have not passed any argument, we do not have any specific function to run over here, so it returns a default value (0x021B2D30) which is the location of the object. In a practical Python program, you probably will not ever need to do this.
Argument i funktioner
An argument is a value that is passed to the function when it is called.
In other words, on the calling side it is an argument, and on the function side it is a parameter.
Lรฅt oss se hur Python arguments work.
Steg 1) Arguments are declared in the function definition. While calling the function, you can pass the values for those arguments as shown below.
Steg 2) To declare a default value for an argument, assign it a value at the function definition.
Exempel: x has no default value. The default value of y=0. When we supply only one argument while calling the multiply function, Python tilldelar det angivna vรคrdet till x medan keeping the value of y=0. Hence the multiply of x*y=0.
Steg 3) This time we will change the value to y=2 instead of the default value y=0, and it will return the output as (4ร2)=8.
Steg 4) You can also change the order in which the arguments are passed in Python. Here we have reversed the order of the values x and y to x=4 and y=2.
Steg 5) Multiple arguments can also be passed as an array. Here in the example we call the multiple args (1,2,3,4,5) by calling the (*args) function.
Exempel: We declared multiple args as the numbers (1,2,3,4,5); when we call the (*args) function, it prints out the output as (1,2,3,4,5).
Tips:
- In Python 2.7, function overloading is not supported. Function overloading is the ability to create multiple methods of the same name with a different implementation. Function overloading is fully supported in Python 3.
- There is quite a confusion between methods and functions. Methods in Python are associated with object instances, while functions are not. When Python anropar en metod, binder den den fรถrsta parametern i det anropet till lรคmplig objektreferens. Med enkla ord, en fristรฅende funktion i Python รคr en "funktion", medan en funktion som รคr ett attribut fรถr en klass eller en instans รคr en "metod".
Hรคr รคr den kompletta Python 3-kod
#define a function
def func1():
print ("I am learning Python function")
print ("still in func1")
func1()
def square(x):
return x*x
print(square(4))
def multiply(x,y=0):
print("value of x=",x)
print("value of y=",y)
return x*y
print(multiply(y=2,x=4))
Hรคr รคr den kompletta Python 2-kod
#define a function
def func1():
print " I am learning Python function"
print " still in func1"
func1()
def square(x):
return x*x
print square(4)
def multiply(x,y=0):
print"value of x=",x
print"value of y=",y
return x*y
print multiply(y=2,x=4)














