Nothing Special   »   [go: up one dir, main page]

ES106 - Module 8-9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

COMPUTER FUNDAMENTALS AND

PROGRAMMING
FUNCTIONS, PARAMETER PASSING, CALL BY
VALUE, RECURSION, LOCAL AND GLOBAL
VARIABLES

A.Y. 2023 - 2024


FIRST SEMESTER
University of Science and Technology of Southern Philippines
OBJECTIVES / INTENDED LEARNING OUTCOMES

At the end of the lesson, students will be able to:


• Students should be able to understand parameter passing in
python and call by value
• Students should be able to understand recursion in python
programming
• Students should be able to differentiate local and global
variables
• Students should be able to understand python GUI programming
and be able to apply it in creating their python program

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


INTRODUCTION

Python is a very popular programming language thanks to its great degree of

readability, widespread adoption and most importantly, its beginner friendliness.

While being incredibly useful for the fields of data science and machine

learning, Python is also great for developing graphical user interfaces. In fact, it

has many frameworks that even beginners can use to easily get started with

developing a GUI.
PYTHON: PARAMETER PASSING

Keyword parameter passing is a technique in Python where arguments are passed to a


function using their parameter names. This allows the arguments to be passed in any order,
as long as their parameter names are specified.
PARAMETER
For example: FUNCTION NAMES

ARGUMENTS

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


IDENTIFIERS

Python utilizes a system, which is known as “Call by Object Reference” or “Call


by assignment”. In the event that you pass arguments like whole numbers,
strings or tuples to a function, the passing is like call-by-value because you
can not change the value of the immutable objects being passed to the
function. Whereas passing mutable objects can be considered as call by
reference because when their values are changed inside the function, then it
will also be reflected outside the function.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: CALL BY VALUE

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: CALL BY VALUE

Binding Names to Objects


In python, each variable to which we assign a value/container is
treated as an object. When we are assigning a value to a variable, we
are actually binding a name to an object.

Name of the objects as


its values are the same.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: CALL BY VALUE

Output: [1, 2, 3, 4]

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


PYTHON: CALL BY VALUE

In the above example, a string which is an immutable type of object is passed as argument
to the function foo. Within the scope of the given function foo, a= “new value” has been
bounded to the same object that string has been bound outside. Within the scope of the function
foo, we modify “old value”` to “new value”. Once we leave the scope of function foo , a=”new
value” is no longer in the name space, and the value that string refers to was never changed.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


RECURSION

WHAT IS RECURSION?

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


RECURSION

Recursion is a common
mathematical and programming
concept. It means that a function
calls itself. This has the benefit of
meaning that you can loop
through data to reach a result.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


RECURSION

Sample Recursion Output


def sum_list(numbers):
if len(numbers) == 0:
return 0
else:
return numbers[0] + sum_list(numbers[1:])

my_list = [1, 2, 3, 4, 5]
result = sum_list(my_list)
print(result)

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


Local Variables

What is LOCAL VARIABLES?

Local variables are those which are initialized inside a


function and belongs only to that specific function. It
cannot be accessed anywhere outside the function. Let’s
see how to create a local variable.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


Local Variables

Sample Output

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


Global Variables

What is GLOBAL VARIABLES?

The global variables are those which are defined outside any
function, and which are accessible throughout the program.

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING


Global Variables

Sample Output

ES106 – COMPUTER FUNDAMENTALS AND PROGRAMMING

You might also like