Skip to Content

Python for Fintech Professionals: Writing our own custom functions

Writing our own functions in Python allows us to encapsulate logic, and improve code clarity.
This is the thirteenth blog on Python for fintch professionals.

In Python, not only can we utilize built-in functions like input(), int(), and print(), but we can also create and publish our own functions to encapsulate frequently used code. This not only enhances code readability but also promotes reusability.

Defining Your Own Function

To define a function in Python, we use the "def" keyword, followed by the function name, a pair of parentheses, and a colon. The naming convention for functions is the same as for variables, and we can include optional or mandatory parameters inside the parentheses.

At the end of the function body, we can use either the "return" keyword to send back a value or simply print the output. The "return" statement can also terminate a function's execution on demand.

def my_function():
x = int(input('Please provide an integer:\n'))
y = 'You entered '
print(y, x)

This function can be invoked by calling "my_function()". Note that since there are no parameters defined, this function cannot accept any arguments; providing one will result in an error.

Remember, a function must be defined before it can be invoked; calling it before defining will not work. Functions perform actions such as displaying output to the console, returning results, or both.

Parameterized Functions

To make functions more dynamic, we typically define them with parameters that accept arguments during runtime.

# Define the function
def my_function(message):
x = int(input('Please provide an integer:\n'))
print(message, x)
# Call the function
my_function('You provided the following value: ')

You can also call the function by assigning a variable to hold the argument:

y = input("You entered: ")
my_function(y)

Default Parameter Values

You can assign default values to parameters, allowing the function to use these defaults if no argument is provided.

def my_function(message='Hi'):
x = int(input('Please provide an integer:\n'))
print(message, x)
my_function()

A function should contain parameters rather than fixed values to remain versatile and dynamic. It's also acceptable to name a variable the same as a parameter; however, this is not necessary.

Parameter vs. Argument

A parameter is defined when creating a function and exists only within that function. An argument is the actual value passed to a parameter at runtime.

For example, consider the print() function, which has positional parameters and optional named parameters (like "sep" and "end"). When you use print(), the values you provide are the arguments for those parameters. You can also change the defaults for named parameters by supplying new arguments.

The Return Instruction

The "return" statement has two forms:

  1. Return without an expression: Stops the function's execution.
  1. Return with an expression: Returns the specified expression.
def custom():
a = 1
b = 2
  c = a + b
  d = c / a
 return c
print(custom())

If you do not specify an expression to return, the function will not return any value.

Conclusion

Creating your own functions in Python can greatly enhance your coding efficiency and creativity. By delving into parameters, arguments, and return values, you can build more complex and dynamic applications.


Share this post
Sign in to leave a comment
Python for Fintech Professionals: Lists
Lists in Python are ordered collections that can hold items of different types.