A function is a set of statements that take inputs, do some specific computation and produces output. The idea is to put some commonly or repeatedly done task together and make a function, so that instead of writing the same code again and again for different inputs, we can call the function.
Python provides built-in functions like print(), etc. but we can also create your own functions. These functions are called user-defined functions.
Pass by Reference or pass by value?
One important thing to note is, in Python every variable name is a reference. When we pass a variable to a function, a new reference to the object is created. Parameter passing in Python is same as reference passing in Java.
[20, 11, 12, 13, 14, 15]
When we pass a reference and change the received reference to something else, the connection between passed and received parameter is broken. For example, consider below program.
[10, 11, 12, 13, 14, 15]
Another example to demonstrate that reference link is broken if we assign a new value (inside the function).
Exercise: Try to guess the output of following code.
A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument.The following example illustrates Default arguments.
('x: ', 10) ('y: ', 50)
Like C++ default arguments, any number of arguments in a function can have a default value. But once we have a default argument, all the arguments to its right must also have default values.
The idea is to allow caller to specify argument name with values so that caller does not need to remember order of parameters.
('Geeks', 'Practice') ('Geeks', 'Practice')
Hello Welcome to GeeksforGeeks
last == Geeks mid == for first == Geeks
Anonymous functions: In Python, anonymous function means that a function is without a name. As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. Please see this for details.
Quick Links :
- Quiz on Python Functions
- Difference between Method and Function in Python
- First Class functions in Python
- Recent articles on Python Functions.
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Log functions in Python
- Python Inner Functions
- Iterator Functions in Python | Set 1
- Decimal Functions in Python | Set 1
- Operator Functions in Python | Set 1
- Python | Functions | Question 4
- Python | Functions | Question 6
- Python | Functions | Question 5
- Operator Functions in Python | Set 2
- Partial Functions in Python
- First Class functions in Python
- Prime functions in Python SymPy
- Array in Python | Set 2 (Important Functions)
- Bisect Algorithm Functions in Python
- Higher Order Functions in Python
- Array in Python | Set 1 (Introduction and Functions)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.