You are on page 1of 3

3/30/2021

Functions Functions
 User Defined Functions  User Defined Functions:

 Definition of a function, building a user-defined function def FTC ():


c = (f-32)*5/9
In Python, a routine (also called as subroutine or subprogram) is known as a user-defined function.
o Running the above code in this form will give absolutely nothing !
The generic form of a user-defined function in Python: o According to the codes above, FTC function is created and ready to be used.

def <functionName>(<optionalParameters>): # notice the parentheses and the ending colon  Calling a user-defined function
<indented statement(s)> # the 'body' of the function
<functionName>(<argument1>, <argument2>, ...)
All the statements that make up the function, called the body of the function, are indented from the def
statement. def FTC ():
Python relies on indenting to show a grouping of lines. The convention is to indent four spaces. c = (f-32)*5/9

A user-defined function converting Fahrenheit to Celsius or vice versa can simply be written as follows: s = FTC(212)
def FTC (): print(s)
c = (f-32)*5/9

def CTF ():


f = (c*9/5)+32

Functions Functions
 User Defined Functions:
 User Defined Functions:
def FTC (f):  Returning values : no value, more than one value
c = (f-32)*5/9 def FTC (f):
c = (f-32)*5/9
s = FTC(212)
return This will return no value.
print(s)
s = FTC(212)
print(s)

Any written user-defined type function will be useless if it does not contain any returning argument/value. Depending nature of the user defined function, more than one value could be returned:

return (<value1>, <value2>, <value3>, ...)


def FTC (f):
c = (f-32)*5/9
return(c)

s = FTC(212) def FTC (f):


return(f-32)*5/9
print(s) This is also possible, not recommended
s = FTC(212)
print(s)

1
3/30/2021

Functions Functions
 User Defined Functions: def FTC (f):  User Defined Functions:
c = (f-32)*5/9
Summary return(c)  Specific and General Variable Names in Calls and Functions
 All user defined Python functions s = FTC(212) New programmers often struggle with creating different names for variables outside of and inside of
o begin with def, print(s) functions.
o followed by the function name,
Local and global variables :
o then inside parentheses a comma-separated list of function arguments.
o and finishing the definition with colon :  Any variable used in the function part is called as local variable. They are invisible outside functions.
 Example : def FTC (f):
 Variables created in the main program code can be considered as global variables.
 The statements to be performed inside the function must be indented.
 At the end of a function it is common to return a value, that is, send a value “out of the
function”.
The def line with the function name and arguments is often referred to as the function header,
while the indented statements constitute the function body.

 To use a function, we must call (or invoke) it. Because the function returns a value, we
need to store this value in a variable or make use of it in other ways.

Functions Functions
 User Defined Functions:  User Defined Functions:

 Changing global variables inside functions


The values of global variables can be accessed inside functions, but the values cannot be changed unless the Function with default parameters
variable is declared as global:

Try not to use the same names in both global and local.
This would make your program confusing !

 Lambda functions
There is a quick one-line construction of functions that is often convenient to make Python code compact:

f = lambda x: x**2 + 4
which is equivalent to:
def f(x):
return x**2 + 4
Lambda functions are usually used to quickly define
a function as argument to another function.

2
3/30/2021

Functions Functions
 User Defined Functions: Unfortunately, there is no way to prevent a constant from being changed. However, there is a widely
accepted Python naming convention for constants.
 Placement of Functions in a Python file
 When defining a variable to be used as a constant, create a name where all the letters are in
Notice that in the examples of functions and function calls, the code that defines the functions is always at uppercase, and separate words are strung together with underscores.
the top of the Python file.
Naming a variable this way serves as a signal to yourself and other programmers that this variable is a
 The main code of the program, which typically incorporates calls to those functions, is written below the constant and its value should never be reassigned.
function definitions.
universalGasConstant = 8.3144621 # It is a constant in any calculation if the unit is in J/molK
 If the main code tries to call a function before it is defined, Python gives an error. universalGasConstant = 2 # with this line its value is changed by an accident for example!
UNIVERSAL_GAS_CONSTANT = 8.3144621 # Defining a constant in this form will create a warning !

 Constants  Finding errors in functions

A constant is a variable whose value does not change throughout a program. When an error occurs at runtime, Python outputs an error message called a traceback.

In Python, there is nothing special about creating a constant. It really is just another variable. If a runtime error happens inside of a function, it is often difficult to find and fix because
o a function may be called from many places in a program.
o different argument values are typically passed in with different calls.

You might also like