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.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- 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 4 (Special Functions and Constants)
- Inverse functions and composition of functions
- Fruitful Functions and Void Functions in Julia
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Partial Functions in Python
- Time Functions in Python | Set 1 (time(), ctime(), sleep()...)
- Calendar Functions in Python | Set 1( calendar(), month(), isleap()...)
- Calendar Functions in Python | Set 2(monthrange(), prcal(), weekday()...)
- Complex Numbers in Python | Set 2 (Important Functions and Constants)
- Complex Numbers in Python | Set 3 (Trigonometric and Hyperbolic Functions)
- Array in Python | Set 2 (Important Functions)
- Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
- Statistical Functions in Python | Set 2 ( Measure of Spread)
- Iterator Functions in Python | Set 1
- Iterator Functions in Python | Set 2 (islice(), starmap(), tee()..)
- Decimal Functions in Python | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.