![]() |
VOOZH | about |
Python functions are reusable blocks of code used to perform a specific task. They help organize programs into smaller sections and execute the same logic whenever needed by calling the function.
A function can be defined using def keyword. Below is the syntax to define a function:
Here, we define a function using def that prints a welcome message when called.
After creating a function, call it by using the name of the functions followed by parenthesis containing parameters of that particular function.
Welcome to GFG
Arguments are values passed to a function when it is called. They allow functions to receive input data and perform operations using those values.
def function_name(arguments):
# function body
return value
Example: In this example, function checks whether the number passed as an argument is even or odd.
Even Odd
Python supports different types of arguments that can be passed during a function call.
1. Default argument: Default argument use a predefined value when no value is passed during the function call.
x: 10 y: 50
Explanation:
2. Keyword Arguments: pass values using parameter names, so argument order does not matter.
Geeks Practice Geeks Practice
Explanation: fname and lname are passed using parameter names and arguments can be provided in any order.
3. Positional Arguments: values are assigned to parameters based on their order in the function call.
Case-1: Hi, I am Olivia My age is 27 Case-2: Hi, I am 27 My age is Olivia
Explanation:
4. Arbitrary Arguments: allow functions to accept multiple values. This is done using two special symbols:
Non-Keyword Arguments (*args): Hey Welcome Keyword Arguments (**kwargs): first == Geeks mid == for last == Geeks
Explanation:
A function defined inside another function is called an inner function (or nested function). It is used to organize related logic and access variables from the outer function.
I love GeeksforGeeks
Return is used to end a function and send a value back to the caller. It can return any data type, multiple values (packed into a tuple), or None if no value is given.
return [expression]
Parameters: expression is the value returned by the function. If no value is returned, it returns None by default.
4 16
Explanation:
Variables refer to objects. Function behavior depends on whether the object is mutable or immutable.
[20, 11, 12, 13] 10
Explanation:
Note: Python uses pass-by-object-reference, where functions receive references to objects instead of actual copies.