Functions

<< Click to Display Table of Contents >>

Navigation:  Scriptiing > Python >

Functions

A function is a device that groups a set of statements so they can be run more than once in a program. Coding an operation as a function makes it a generally useful tool, which we can use in a variety of contexts.

 

def name(arg1, arg2,... argN):

 statements

 

Functions can have an optional return statement.

 

def name(arg1, arg2,... argN):

 ...

 return value

 

Example

Defining the function

# Get file name to save to

def GetFileFile():

 dialog = SaveFileDialog()

 filter = 'Text files (*.txt)|*.txt|All files (*.*)|*.*'

 dialog.Filter = filter

 dialog.Title = 'Save Parts'

 dialog.FileName = design.ShortName

 result = dialog.ShowDialog()

 if result == DialogResult.OK:

         return dialog.FileName

 else:

         return None

 

Calling the function

fileName = GetFileFile()