Functions in Python:Function is a block of code that is used to perform single action. Functions are reusable. Python has many built-in functions, in additional to that it allows the user to define their own functions which are called as user defined functions.
Defining a function in python:Python have simple rules to define a function.
• Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
• The arguments should be placed within these parentheses. we can also define parameters inside these parentheses.
• The first statement of a function can be an optional statement - the documentation string of the function or docstring.
• The code block within every function starts with a colon (:) and is indented.
• The statement return exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return none.
Syntax of the function declaration:
def Namefthefunction( list of parameters separated by comma): "function_docstring" function_suite return [expression]
Example of the Function:Let us see the below example which accepts two numers as an arguments and returns another number type as the result
def AddNums(val1, val2): val3 = val1+val2 "This prints a sumbe of the above two numbers" print val3 return val3
Function calling in python:After defining the function as shown above, that specifies the parameters that are to be included in the function and structures the block of the code
def AddNums(val1, val2): val3 = val1+val2 "This prints a sumbe of the above two numbers" print val3 return val3 #calling the function Result =AddNums (20, 30)
The output of the above code is
Function parameter types python:
There are four types of arguments types available in python
Required Arguments:Required arguments are the arguments passed to a function in correct positional order. Here, the number of arguments in the function call should match exactly with the function definition.
Keyword Arguments:Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.
Default arguments:A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.
Variable-Length Arguments:When we need to process a function for more arguments than you specified while defining the function. This type of arguments are called variable-length arguments and are not named in the function definition, unlike required and default arguments.
AS we know Pthon is strongly typed because every object has a type, every object knows its type, it's impossible to accidentally or deliberately use an object of a type "as if" it was an object of a different type, and all elementary operations on the object are delegated to its type.
This has nothing to do with names. A name in Python doesn't "have a type": if and when a name's defined, the name refers to an object, and the object does have a type