User-Defined Functions in Python

Functions are common to all programming languages, and it can be defined as a block of re-usable code to perform specific tasks. But defining functions in Python means knowing both types first: built-in and user-defined. Built-in functions are usually a part of Python packages and libraries, whereas user-defined functions are written by the developers to meet certain requirements. In Python, all functions are treated as objects, so it is more flexible compared to other high-level languages.
In this article, we will focus on the user-defined functions in Python. To completely understand the concept, we will learn how they can be implemented by writing code examples. Let’s have a look at other important concepts before jumping into coding.

Importance of user-defined functions in Python

In general, developers can write user-defined functions or it can be borrowed as a third-party library. This also means your own user-defined functions can also be a third-party library for other users. User-defined functions have certain advantages depending when and how they are used. Let ‘s have a look at the following points.
  • User-defined functions are reusable code blocks; they only need to be written once, then they can be used multiple times. They can even be used in other applications, too.
  • These functions are very useful, from writing common utilities to specific business logic. These functions can also be modified per requirement.
  • The code is usually well organized, easy to maintain, and developer-friendly. Which means it can support the modular design approach.
  • As user-defined functions can be written independently, the tasks of a project can be distributed for rapid application development.
  • A well-defined and thoughtfully written user-defined function can ease the application development process.
Now that we have a basic understanding of the advantages, let’s have a look at different function arguments in Python.

Function arguments in Python

In Python, user-defined functions can take four different types of arguments. The argument types and their meanings, however, are pre-defined and can’t be changed. But a developer can, instead,  follow these pre-defined rules to make their own custom functions. The following are the four types of arguments and their rules.

1. Default arguments:

Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during function call. The default value is assigned by using assignment (=) operator. Below is a typical syntax for default argument. Here, msg parameter has a default value Hello!.

Function definition

def defaultArg( name, msg = "Hello!"):

Function call

defaultArg( name)

2. Required arguments:

Required arguments are the mandatory arguments of a function. These argument values must be passed in correct number and order during function call. Below is a typical syntax for a required argument function.

Function definition

def requiredArg (str,num):

Function call

requiredArg ("Hello",12)

3. Keyword arguments:

Keyword arguments are relevant for Python function calls. The keywords are mentioned during the function call along with their corresponding values. These keywords are mapped with the function arguments so the function can easily identify the corresponding values even if the order is not maintained during the function call. The following is the syntax for keyword arguments.

Function definition

def keywordArg( name, role ):

Function call

keywordArg( name = "Tom", role = "Manager")
or
keywordArg( role = "Manager", name = "Tom")

4. Variable number of arguments:

This is very useful when we do not know the exact number of arguments that will be passed to a function. Or we can have a design where any number of arguments can be passed based on the requirement. Below is the syntax for this type of function call.

Function definition

def varlengthArgs(*varargs):

Function call

varlengthArgs(30,40,50,60)
Now that we have an idea about the different argument types in Python. Let’s check the steps to write a user-defined function.

Writing user-defined functions in Python

These are the basic steps in writing user-defined functions in Python. For additional functionalities, we need to incorporate more steps as needed.
  • Step 1: Declare the function with the keyword def followed by the function name.
  • Step 2: Write the arguments inside the opening and closing parentheses of the function, and end the declaration with a colon.
  • Step 3: Add the program statements to be executed.
  • Step 4: End the function with/without return statement.
The example below is a typical syntax for defining functions:
def userDefFunction (arg1, arg2, arg3 ...):
    program statement1
    program statement3
    program statement3
    ....
   return;

Let’s try some code examples

In this section, we will cover four different examples for all the four types of function arguments.

Default arguments example

The following code snippet represents a default argument example. We have written the code in a script file named defArg.py
Listing 1: Default argument example
def defArgFunc( empname, emprole = "Manager" ):   
   print ("Emp Name: ", empname)
   print ("Emp Role ", emprole)
   return;
print("Using default value")
defArgFunc(empname="Nick")
print("************************")
print("Overwriting default value")
defArgFunc(empname="Tom",emprole = "CEO")
Now run the script file as shown below. It will display the following output:
user-defined functions in python

Required arguments example

The code snippet below represents a required argument example. We have written the code in a script file named reqArg.py
Listing 2: Required argument example
def reqArgFunc( empname):   
   print ("Emp Name: ", empname)
   return;   
print("Not passing required arg value")
reqArgFunc()
print("Now passing required arg value")
reqArgFunc("Hello")
Now, first run the code without passing the required argument and the following output will be displayed:
user-defined functions in python
Now comment out reqArgFunc() function call in the script, and run the code with the required argument. The following output will be displayed:
user-defined functions in python

Keyword arguments example

Below is an example of keyword argumentcode snippet. We have written the code in a script file named keyArg.py
Listing 3: Keyword argument example
def keyArgFunc(empname, emprole):   
   print ("Emp Name: ", empname)
   print ("Emp Role: ", emprole)
   return;   
print("Calling in proper sequence")
keyArgFunc(empname = "Nick",emprole = "Manager" )
print("Calling in opposite sequence")
keyArgFunc(emprole = "Manager",empname = "Nick")
Now run the script file as shown below. It will display the following output:
user-defined functions in python

Variable number of arguments example

The code snippet below shows an example of a variable length argument. We have written the code in a script file named varArg.py
Listing 4: Variable length argument example
def varLenArgFunc(*varvallist ):   
   print ("The Output is: ")
   for varval in varvallist:
      print (varval)
   return;   
print("Calling with single value")
varLenArgFunc(55)
print("Calling with multiple values")
varLenArgFunc(50,60,70,80)
Once you run the code, the following output will be displayed:
user-defined functions in python

Conclusion


In this article, we have discussed the different aspects of user-defined functions in Python. We have also explored how user-defined functions can be written in simple steps.

Exchange Store - Eating Memory

Problem

Before you go any further with this article THIS IS COMPLETELY NORMAL! It may look a little odd if you look in Task Manager.
store.exe at 99%
Store.exe will take as much RAM as it can get hold of, the difference between that, and a program that ‘hogs’ memory is, it has been designed to monitor the system, and release memory as it’s required by other processes.
So for 99.999% of you stop reading now!
OK, You can cap the memory that Exchange uses, but Microsoft recommend that you don’t do this. Why did I do it then? Well my company uses N-central to monitor our clients servers, and when monitoring the metrics for memory, it reports as failed if it stays at 99% for a while. This can happen quite often on some busier Exchange servers. So I set about capping the memory that store.exe would use.

Solution

1. Launch ADSIEdit.msc > Connect to the Configuration Context.
Launch ADsiedit
2. Navigate to;
Services > Microsoft Exchange > ‘Organization Name’ >  Administrative Groups > ‘Administrative Group Name’ > Servers > < ‘Server Name’ > InformationStore
Open its properties > Locate the msExchESEParamCacheSizeMax attribute. Edit its value.
Cap Store.exe memory

IMPORTANT:

After Exchange 2010 SP1 you also need to set the msExchESEparamCacheSizeMin  to the SAME VALUE.
3. Finaly restart the Microsoft Exchange Information Store service.
Restart Information Store

Display System Information on Your Desktop with Desktop Info

Do you like to monitor your system but do not want a complicated app to do it with? If you love simplicity and easy configuration then join us as we look at Desktop Info.
Desktop Info in Action
Desktop Info comes in a zip file format so you will need to unzip the app, place it into an appropriate “Program Files Folder”, and create a shortcut. Do NOT delete the “Read Me File”…this will be extremely useful to you when you make changes to the “Configuration File”. Once you have everything set up you are ready to start Desktop Info up.
desktop-info-01
This is the default layout and set of listings displayed when you start Desktop Info up for the first time. The font colors will be a mix of colors as seen here and the font size will perhaps be a bit small but those are very easy to change if desired.
desktop-info-02
You can access the “Context Menu” directly over the “information area”…so no need to look for it in the “System Tray”. Notice that you can easily access that important “Read Me File” from here…
desktop-info-03
The full contents of the configuration file (.ini file) are displayed here so that you can see exactly what kind of information can be displayed using the default listings. The first section is “Options”…you will most likely want to increase the font size while you are here.
desktop-info-04
Then “Items”… If you are unhappy with any of the font colors in the “information area” this is where you can make the changes. You can turn information display items on or off here.
desktop-info-05
And finally “Files, Registry, & Event Logs”.
desktop-info-06
Here is our displayed information after a few tweaks in the configuration file. Very nice.
desktop-info-07
Conclusion
If you have been looking for a system information app that is simple and easy to set up then you should definitely give Desktop Info a try.
Links

Cool Server Management Application

All software on this page is written by Glenn Delahoy for the Windows platform and is free to download and use. None of these applications are crippled (not intentionally anyway), time limited, contain nags, ads, spyware or other such intrusions. All applications are in English only except where noted.
This software is no longer supported. It is provided as is. It may or may not work for you.

Windows: 2000/XP/Vista/Win7
A small calendar and event reminder that unobtrusively sits on your desktop and is ready to use any time. No taskbar button or system tray icon. The calendar is transparent so it's not an eyesore and consumes very little memory. Enter as many daily or regular reminders as you like. Float your mouse over a day to see all reminders for the day. Shows a short list of upcoming reminders below the calendar. Fonts, colours and tooltips are customisable so it blends in with your desktop colour and font schemes.
Windows: 2000/XP/Vista/2003/2008/Win7/Win8
This little application displays system information on your desktop in a similar way to some other desktop information tools. Unlike others, this application looks like wallpaper but stays resident in memory and continues to update the display in real time. Uses very little memory and nearly zero cpu. Perfect for quick identification and walk-by monitoring of production or test server farms. Everything is customisable including language.
Windows: .Net 3.5
This is my first C#/.NET application, wahoo! Disk Usage Analyser is a Windows Explorer clone but with tools to help you track down disk usage. It scans the selected disk to generate statistics on size, files and folders. A configurable list of preset filters can be used to show only files of specific types. Reports can be generated in text, csv, xml and html format. An optional disk monitor will update the display in real time when changes are made to the folders on display. Disk Usage Analyser is written for .Net v3.5. Works for 32 bit and 64 bit Windows.
Windows: All versions
Simple file splitter and joiner to help you move those huge files across disks, around the network or burn to disk. You control the split size according to your requirements. The results are checksummed to ensure a successful split and rejoin. Handles huge file sizes and can create splits up to 2GB in size.
Reference

http://www.glenn.delahoy.com/software/

How to Unblock a Mobile Device in Exchange Server

When a mobile device is being blocked by Exchange ActiveSync, there are two ways that you can unblock it. The first is using the Exchange admin center for Exchange 2013/2016. Navigate to the mailboxes view, select the mailbox in question, and in the right-hand pane click the View details link under Mobile Devices. The list of mobile devices associated with at user will be displayed, and you can highlight the device you want to unblock and click the Allow button, then save the change.
exchange-activesync-unblock-device
This change will add the device to the user's allowed devices list. If you'd prefer to just remove the device association, click the delete button instead.
The other method is to use the Exchange Management Shell. Run the Get-CASMailbox cmdlet to see the blocked device IDs for the user.
To add a blocked device ID to the allowed device ID list, use the Set-CASMailbox cmdlet.
Alternatively, you can just remove it from the blocked device ID list.
Or, if you'd prefer to just remove the device association, use Remove-MobileDevice.