CE-111 Introduction To Computing For Civil Engineers
CE-111 Introduction To Computing For Civil Engineers
Introduction to
Computing for Civil
Engineers
Lecture 6
Recap
• Control Stuctures
– Conditional Statement
– Indefinite and definite iteration
• while loop
• for loop
Lecture 05 2
Road Map for Today
• Function programming in python
Lecture 05 3
2.10 WRITING FUNCTIONS IN PYTHON
Lecture 05 4
Mathematical Concept of Functions
• A function is a relationship or mapping between one or more
inputs and a set of outputs.
• In mathematics a function is represented as
𝑧𝑧 = 𝑓𝑓 𝑥𝑥, 𝑦𝑦
• Here, 𝑓𝑓 is a function that operates on the inputs 𝑥𝑥 and 𝑦𝑦.
• The output of the function is 𝑧𝑧.
Lecture 05 5
Functions in programming
• In programming, a function is a self-contained block of code that
encapsulates a specific task or related group of tasks.
• Till now you have been introduced with some of the built-in functions
provided by Python like in the last week we used the range function.
• Each of the built-in functions performs a specific task.
• The code that accomplishes the task is defined somewhere, but you
don’t need to know where or even how the code works.
• All you need to know about is the function’s interface:
1. What arguments (if any) it takes
2. What values (if any) it returns
Lecture 05 6
Importance of Python Functions
• Abstraction and Reusability
– Suppose you write some code that does something useful.
– As you continue development, you find that the task performed by that code is
one you need often, in many different locations within your application. What
should you do?
– Well, you could just replicate the code over and over again, using your editor’s
copy-and-paste capability.
– Later on, you’ll probably decide that the code in question needs to be
modified.
– You’ll either find something wrong with it that needs to be fixed, or you’ll want
to enhance it in some way.
– If copies of the code are scattered all over your application, then you’ll need to
make the necessary changes in every location.
Lecture 05 7
Importance of Python Functions
• Modularity
– Functions allow complex processes to be broken up into smaller steps.
Lecture 05 8
Importance of Python Functions
• Namespace separation
– A namespace is a region of a program in which identifiers have meaning.
– when a Python function is called, a new namespace is created for that function,
one that is distinct from all other namespaces that already exist.
– The practical upshot of this is that variables can be defined and used within a
Python function even if they have the same name as variables defined in other
functions or in the main program.
– In these cases, there will be no confusion or interference because they’re kept
in separate namespaces.
– This means that when you write code within a function, you can use variable
names and identifiers without worrying about whether they’re already used
elsewhere outside the function.
– This helps minimize errors in code considerably.
Lecture 05 9
Syntax of Function Calls and Definition
Explanation of the components of the
Syntax definition
• Syntax for defining a Python Function
Lecture 05 11
Example (cont…)
Lecture 05 12
Local Variables
• Local variable: variable that is assigned a value inside a
function
– Belongs to the function in which it was created
• Only statements inside that function can access it, error will occur if another
function tries to access the variable
• Scope: the part of a program in which a variable may be
accessed
– For local variable: function in which created
Lecture 05 13
Local Variables (cont’d.)
• Local variable cannot be accessed by statements inside its
function which precede its creation
• Different functions may have local variables with the same
name
– Each function does not see the other function’s local variables, so no
confusion
Lecture 05 14
Passing Arguments to Functions
• Argument: piece of data that is sent into a function
– Function can use argument in calculations
– When calling the function, the argument is placed in parentheses
following the function name
Lecture 05 15
Passing Arguments to Functions (cont’d.)
Lecture 05 16
Passing Arguments to Functions (cont’d.)
• Parameter variable: variable that is assigned the value of an
argument when the function is called
– The parameter and the argument reference the same value
– General format:
def function_name(parameter) :
– Scope of a parameter: the function in which the parameter is used
Lecture 05 17
Passing Arguments to Functions (cont’d.)
Lecture 05 18
Passing Multiple Arguments
• Python allows writing a function that accepts multiple
arguments
– Parameter list replaces single parameter
• Parameter list items separated by comma
• Arguments are passed by position to corresponding
parameters
– First parameter receives value of first argument, second parameter
receives value of second argument, etc.
Lecture 05 19
Passing Multiple Arguments (cont’d.)
Lecture 05 20
Making Changes to Parameters
• Changes made to a parameter value within the function do not
affect the argument
– Known as pass by value
– Provides a way for unidirectional communication between one
function and another function
• Calling function can communicate with called function
Lecture 05 21
Making Changes to Parameters (cont’d.)
Lecture 05 22
Making Changes to Parameters (cont’d.)
• Figure 3-18
– The value variable passed to the change_me function cannot be
changed by it
Lecture 05 23
Keyword Arguments
• Keyword argument: argument that specifies which parameter
the value should be passed to
– Position when calling function is irrelevant
– General Format:
function_name(parameter=value)
• Possible to mix keyword and positional arguments when calling
a function
– Positional arguments must appear first
Lecture 05 24
Global Variables and Global Constants
• Global variable: created by assignment statement written
outside all the functions
– Can be accessed by any statement in the program file, including from
within a function
– If a function needs to assign a value to the global variable, the global
variable must be redeclared within the function
• General format: global variable_name
Lecture 05 25
Global Variables and Global Constants (cont’d.)
• Reasons to avoid using global variables:
– Global variables making debugging difficult
• Many locations in the code could be causing a wrong variable value
– Functions that use global variables are usually dependent on those
variables
• Makes function hard to transfer to another program
– Global variables make a program hard to understand
Lecture 05 26
Global Constants
• Global constant: global name that references a value that
cannot be changed
– Permissible to use global constants in a program
– To simulate global constant in Python, create global variable and do
not re-declare it within functions
Lecture 05 27
Summary
• This topic covered:
– The advantages of using functions
– The syntax for defining and calling a function
– Methods for designing a program to use functions
– Use of local variables and their scope
– Syntax and limitations of passing arguments to functions
– Global variables, global constants, and their advantages and
disadvantages
Lecture 05 28