Python

Constant dripping wears away a stone

Functions


A functionis a block of statements put as a unit to perform a specific task. A function will be executed when it is called. A function in a program can be called multiple times whenever the specific task needs to be performed.
  • Creating a Function: A function can be created using the keyworddeffollowed by the name of the function. Inside the parentheses following the function name are one or more parameters.
  • Parameters: Parameters are the variables that are put inside the parentheses following the function name when the function is created.
  • Calling a Function: A function can be called using the function name followed by parentheses. If there are parameters in the function, the corresponding arguments need to be enclosed inside the parentheses.
  • Function Arguments: Function arguments are values that are passed to the parameters when call the function.
    • Number of Arguments: When call a function, the same number of arguments as the number of parameters of the function need to be used. Otherwise, an error will be raised and reported.
    • Arbitrary Arguments: If using an asterisk * before the parameter name in a function definition, the number of arguments passed to the parameter can be arbitrary, aka arbitrary arguments. The arguments will be passed to the function with a tuple such that the function will receive a tuple of arguments, and can access to the tuple items with tuple item indexings.
    • Keyword Arguments: Arguments can also be passed to the parameters of a function with key = valuesyntax. In this case, because keys are specified, the order of the arguments does not matter.
    • Arbitrary Keyword Arguments: If the number of key arguments are unknown, put two asterisks ** before the parameter in the function definition. The arguments will be passed to the function in the form of a dictionary such that the function will receive a dictionary of key-value arguments, and can access to the values with the corresponding keys in the dictionary.
    • Default Parameter Value: When define a function, its parameters can be assigned default values. If we call such function without passing arguments, the function will use the default values for the parameters.
  • "Empty" Function: If a function has no content, put the keywordpassas a statement inside the function to avoid getting an error.
  • Return a Value: A function can return a value using areturnstatement.
  • Recursion: If a function calls itself, it is called a recursion. A recursive function can make the programming look very efficient and easy to understand. However, time and memory wise, a recursive function may be much less efficient than the corresponding iterative function. Also, you need to pay extra attention to making sure the recursion can terminate.
  • This following is a recursive function for generating the first 50 Fibonacci numbers:

    Below is the corresponding iterative function for generating the first 50 Fibonacci numbers. As we can see, the time used for the recursive function is much longer than the time used for the iterative function. This is because with the recursive function, every Fibonacci number needs to be recalculated every time it is used. However, in the iterative function, each Fibonacci number will be stored in a list after it is calculated. When the calculation of a Fibonacci number that appears later in the sequence needs to use a Fibonacci number that appears earlier in the sequence, the program can just fetch the number from the list so it does not need to recalculate the number.