Introduction to Python Functions

INTRODUCTION TO PYTHON FUNCTIONS

Introduction
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions. Functions are an essential part of the Python programming language: you might have already encountered and used some of the many fantastic functions that are built-in in the Python language or that come with its library ecosystem. However, as a Data Scientist, you’ll constantly need to write your own functions to solve problems that your data poses to you.

Rules Governing Python Functions

  1. Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
  2. Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
  3. The first statement of a function can be an optional statement - the documentation string of the function or docstring.
  4. The code block within every function starts with a colon (:) and is indented.
  5. The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None. Types of Functions
  6. Built-in functions, such as help() to ask for help, min() to get the minimum value, print() to print an object to the terminal,… You can find an overview with more of these functions here.
  7. User-Defined Functions (UDFs), which are functions that users create to help them out; And
  8. Anonymous functions, which are also called lambda functions because they are not declared with the standard def keyword.

Syntax
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
By default, parameters have a positional behavior and you need to inform them in the same order that they were defined.
Calling a Function
Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code.
Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme() function −
Function Arguments in Python
Earlier, you learned about the difference between parameters and arguments. In short, arguments are the things which are given to any function or method call, while the function or method code refers to the arguments by their parameter names. There are four types of arguments that Python UDFs can take:
• Default arguments
• Required arguments
• Keyword arguments
• Variable number of arguments

Default Arguments
Default arguments are those that take a default value if no argument value is passed during the function call. You can assign this default value by with the assignment operator =, just like in the following example:

Define plus() function

def plus(a,b = 2):
return a + b

Call plus() with only a parameter

plus(a=1)

Call plus() with a and b parameters

plus(a=1, b=3)
Required Arguments
As the name kind of gives away, the required arguments of a UDF are those that have to be in there. These arguments need to be passed during the function call and in precisely the right order, just like in the following example:

Define plus() with required arguments

def plus(a,b):
return a + b

Keyword Arguments
If you want to make sure that you call all the parameters in the right order, you can use the keyword arguments in your function call. You use these to identify the arguments by their parameter name. Let’s take the example from above to make this a bit more clear:

Define plus() function

def plus(a,b):
return a + b

Call plus() function with parameters

plus(2,3)

Call plus() function with keyword arguments

plus(a=1, b=2)

Variable Number of Arguments
In cases where you don’t know the exact number of arguments that you want to pass to a function, you can use the following syntax with *args:

Define plus() function to accept a variable number of arguments

def plus(*args):
return sum(args)

Calculate the sum

plus(1,4,5)

7