Comp Sci
Classified in Computers
Written at on English with a size of 5.83 KB.
Functions, and Arguments
-A function is a module of program code that takes input(arguments) and prodcues output (return value)
arguments allow us to customize the operation performed by the function
return value is often the result of executing the code
Calling a function causes the code in a function to execute
(if called again, it will execute again)
To document a function, use a multi-line comment immediately after the def line
Local Variable
-Variables used inside functions
accessible/usuable within that function only
refers to the variable's scope
Global Variable
Variable used outside of a function
(global variables' scope includes both inside and outside of functions
– Global variables' scope includes both inside and outside of
functions
– However, since there could be naming conflicts, we have
to explicitly declare when we use global variables
Arguments
Values passed into (and sometimes out of) functions
-used to customize the behaviour of the function
Pass by Value
one way that arguments are passed into a function is by value
(the value passed to the function when it is called is copied, the copy is put into the argument variable)
(argument variable has the same scope as a local variable)
Advantage:
-When calling a function by passing its arguments from
variables, you don't have to worry about those
variables' values being modified
Pass by Reference
Another way is also passing argument values by reference
– This is possible in C++ with the & operator
– By reference means that the values become linked via the
argument variable
• In other words, the argument becomes an alias for the value
• Advantage:
– You can pass values to functions that you intend to be
modified
Slack (important data structure)
A collection of items
-items can only be inserted at the top of the stack
-can only be removed from the top
-used when functions are called(each time a function is called, python pushes a new itme (stack frame)onto the calling stack)
-A stack frame contains space for all arguents and local variables
(LIFO, last in, first out)
push (insertion)bottom to top
pop (delete)top to bottom
Recursion
A function that is defined in terms of itself
-The example below is called direct recursion, since the
function forever calls itself directly
Python:
def forever():
print("hello")
• The example below is called indirect recursion, since
the function forever1 calls itself indirectly
– This example has two functions calling each other in a
cycle, but any number of functions can be involved
def forever1():
print("hello")
forever2()
def forever2():
forever1()
forever()
-Include exit condition as a way to stop repetition in recursion
-if not it will be an infinite recursion
Fibonacci numbers are another function that can be
easily created with recursion
– Recall that Fibonacci numbers start with 0 and 1, then each
number of the sum of the previous two numbers
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
Tail recursion
a special case of recersion where the recursive call is the last thing to happen before the function return
-can be easily optimized:
converting to an iterative equivalent
def printNTimes(n, message):
if n == 0:
return
print(message)
printNTimes(n-1, message)
?
def printNTimes(n, message):
for i in range(n):
print(message)
simplifying the calling stack
Each stack frame on the calling stack remebers the return addres, which is the address of the instruction immediately
-since the very next action after the recursive call is a return, we can simplify the stack significantly
-instead of each recursive call waiting for other recursive calls to exits only to return, u can just have the innermost recursive call return to the original return address
print(fibonacci(5))