PowerPoint Presentation
Basics in programming
Lectures 8.
Functions
Scope of variables
Mutable and Immutable data
WhyWrite Functions?
The ability to define our own functions helps programmers in two ways. When you are
writing a program if you find yourself writing the same code more than once, it is probably
best to define a function with the repeated code in it. Then you can call the function as
many times as needed instead of rewriting the code again and again.
-> LESS ERRORS
What is a function?
(1) In programming, a named section of a program that performs a specific task. In this sense, a function is a type of procedure or routine. Some programming languages make a distinction between a function, which returns a value, and a procedure, which performs some operation but does not return a value.
Most programming languages come with a prewritten set of functions that are kept in a library. You can also write your own functions to perform specialized tasks.
What is a function?
(2) The term function is also used synonymously with operation and command. For example, you execute the delete function to erase a word
Source: https://www.webopedia.com/TERM/F/function.html
Most simple function in Python
Most simple function in Python
WhyWrite Functions?
The ability to define our own functions helps programmers in two ways.
When you arewriting a program if you find yourself writing the same code more than once, it is probably
best to define a function with the repeated code in it.
Then you can call the function asmany times as needed instead of rewriting the code again and again.
-> LESS ERRORS
WhyWrite Functions?
Writing functions also helps make our code easier to read.
When we use good names for variables and functions in our programs we can read the code and understand what we have written not only as we write it, but years later when we need to look at the code we wrote again.
WhyWrite Functions?
It is important for others in the group to be able to read and understand the code we have written.
Writing functions can lead to nice modularized code that is much easier to maintain by you and by others in a group.
-> MAINTAINABLE CODE
Why Write Functions?
Functions on Code Complete 2
Functions
Functions are something most of us are familiar with from Mathematics. A function g(x) might be defined as
When using function we pass value of x and we get result from it
Functions
The identifier g represents the definition of a function and calling a function by writing g(6) is called function application or a function call.
These two concepts are part of most programming languages including Python. In Python, functions can be both defined and called.
Functions
Function above equals in Python:
Functions
To call a function in Python we write g(6) for instance, just they way we do in Mathematics.
It means the same thing, too. Executing g(6) means calling the function g with the value 6 to compute the value of the function call.
Functions
Function in Python can do more than a function in Mathematics. Functions can execute statements as well as return a value.
In Mathematics a value is computed and returned. There are no side-effects of calling the function.
In Python (and in just about any programming language), there can be side-effects.
Functions
A function can contain more than one statement just like our programs contain statements.
Functions
Functions in Python
Functions in Python
Passing values to function
Passing values to function (default)
Returning values from function
Functions
When writing a function we need to decide:
What should our function be called?
What should we give to our function?
What should the function do?
Finally, what should our function return?
Functions
Create simple calculator by using functions and user input
Scope of Variables
When writing functions it is important to understand scope.
Scope refers to the area in a program where a variable is defined. Normally, a variable is defined after it has been assigned a value.
You cannot reference a variable until it has been assigned a value.
Scope of Variables
Local Scope
Enclosing Scope
Global Scope
Built-in Scope.
Scope of Variables
Local Scope
Local scope extends for the body of a function and refers to anything indented in the function definition.
Variables, including the parameter, that are defined in the body of a function are local to that function and cannot be accessed outside the function.
They are local variables.
Scope of Variables
Global Scope
A variable which is defined in the main body of a file is called a global variable.
It will be visible throughout the file, and also inside any file which imports that file.
Global variables can have unintended consequences because of their wide-ranging effects – that is why we should almost never use them. Only objects which are intended to be used globally, like functions and classes, should be put in the global namespace.
Scope of Variables
Built-in Scop
The final scope rule is the built-in scope. The built-in scope refers to those identifiers
that are built-in to Python. For instance, the len function can be used anywhere in a program
to find the length of a sequence.
Mutable Data and Functions
If function changes data of a variable that is passed to it as argument its called mutating.
Some variables are immutable and some mutable in functions.
Mutable Data and Functions
Mutable Data and Functions
When a function is called that mutates one or more of its parameters, the calling code will see that the data has been mutated.
The mutation is not somehow undone when the function returns. Since strings, ints, floats, and bools are all immutable, this never comesup when passing arguments of these types.
But, again, lists are mutable, and a function may mutate a list
Mutable Data and Functions
Mutable Data and Functions
Try mutating int
Mutable and Immutable objects
https://www.pythonforthelab.com/blog/mutable-and-immutable-objects/#mutable-and-immutable-data-types
Excercises 6.
Create program that uses function to CAPITALISE name that is given to it by user input. Also create another function that calculates length of tha name by using for-loop
Create program that asks user for 5 numbers and then uses function to calculate power of 1,2,3,4,5 for each number and prints result.
Next classes
Variable-length arguments in functions
Recursion in functions
Objects
Functions
More datatypes ( tuples, sets, dictionaries..)
Modules
/docProps/thumbnail.jpeg