Functions, Arguments, and Recursion in Programming

Classified in Computers

Written at on English with a size of 4.58 KB.

Functions and Arguments

A function is a module of program code that takes input (arguments) and produces output (a return value).

  • Arguments allow us to customize the operation performed by the function.
  • The 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 Variables

Local variables are variables used inside functions.

  • They are accessible/usable within that function only.
  • This refers to the variable's scope.

Global Variables

Global variables are variables used outside of a function.

  • 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

Arguments are values passed into (and sometimes out of) functions.

  • They are used to customize the behavior 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, and the copy is put into the argument variable.
  • The 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 to pass argument values is by reference.

  • This is possible in C++ with the & operator.
  • Pass 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.

Stack (Important Data Structure)

A stack is a collection of items with the following properties:

  • Items can only be inserted at the top of the stack.
  • Items can only be removed from the top.
  • Used when functions are called (each time a function is called, Python pushes a new item (stack frame) onto the calling stack).
  • A stack frame contains space for all arguments and local variables.
  • Follows the LIFO principle (Last In, First Out).

Operations:

  • Push (Insertion): Bottom to top
  • Pop (Delete): Top to bottom

Recursion

Recursion is when a function is defined in terms of itself.

  • Direct Recursion: The function forever calls itself directly. For example:
def forever():
    print("hello")
    forever()
  • Indirect Recursion: The function calls itself indirectly. For example:
def forever1():
    print("hello")
    forever2()

def forever2():
    forever1()
    forever()
  • This example has two functions calling each other in a cycle, but any number of functions can be involved.
  • Include an 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 is the sum of the previous two numbers.
def fibonacci(n):
    if n 

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))

Entradas relacionadas: