A group of statements that perform a given task is a function. They help separate specific logic and organize the code. They can be called repeatedly to perform the same task over and over again.
Table of Contents:
- Function Definitions
- Passing values to Functions
- Scope of names
- Defining Functions
- Recursive functions
def func(a, b, c):
statement_1
statement_2
..
return(v)A function definition includes the following:
- Function Name (
func) - Arguments/Parameters (
a,b,c) - A
returnstatement, exits the function and returns a value. - If there is no
return, function ends when last statement is reached.
Same as defining the function, instead actual values are passed into the arguments.
def twice(n):
return(2*n)
print(twice(12)) # 24Names within the function have local scope.
- A function must be defined before it is invoked.
- Interpreter reads the code from top to bottom.
- Functions are not read, but their definition is remembered.
- They're read when the function is invoked.
- A function can all itself.
def factorial(n):
if n <= 0:
return(1)
else:
val = n * factorial(n-1)
return(val)