- Neater code
- Modular code
- Easier to debug
def <function-name>(<parameters>):
""" Function's docstring """
<Expressions/Statements/Instructions>
return <expression/value>
- ends with colon
- everything inside the function should follow proper indentation
- docstring is optional
- python doesn't require you to specify the datatype of the variable
Calling a function
<function-name> (<value-to-be-passed-as-argument>)
def add(a,b):
return a + b
add(5,7)
- This calls the function and adds 5,7 ie returns 13
Arguments and Parameters
- in the above code
(a,b) required to be filled with some integers, the integers in question are the arguments, arguments keep changing
- the
(a,b) in question are the parameters, they're in the function definition
Types of functions
User Defined
#User-defined function add.
def add(a,b):
#Inbuilt function print.
print(a + b)
#Driver's code
z = add(2,3)
print(z)
#output : 5
Inbuilt