Functions Procedures and Abstraction Dr José M Reyes Álamo Status of Assignment and Quizzes Past Due RA1ComputerConcepts RA2PythonCh1Ch2 RA3PythonCh5Ch7 Quiz1 Pending RA4PythonCh3Ch6 RA5PythonCh8Ch10 ID: 772894
Download Presentation The PPT/PDF document "Functions, Procedures, and Abstraction" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo
Status of Assignment and Quizzes Past Due RA1_ComputerConcepts RA2_PythonCh1Ch2 RA3_PythonCh5Ch7Quiz1 Pending RA4_PythonCh3Ch6 RA5_PythonCh8Ch10 Labs Lab1 Lab2 Lab3 Lab4
Outline Functions Built-in functions User defined functionsAbstraction ReusabilityParameters and arguments Returning values Variables Scope
Functions
Functions From mathematics, a functions perform some operation and returns a result. Functions hide the details of and operation, to make it easy to use by others e.g. sqrt () for computing the square root.
Function In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements . You can invoke the function by name .
Built in functions
Python provides built-in functions Type conversion functions
A big list of built-in functions http://docs.python.org/library/functions.html
Remember the Math module? Math functions
Lots of Python modules available Built-in modules The Python Standard Library: http://docs.python.org/library/ Other modulesPyPI - the Python Package Index http://pypi.python.org/pypiThe Python Package Index is a repository of software for the Python programming language. There are currently 24018 packages there.
User defined functions
Mathematical notation Consider a function which converts temperatures from Celsius to temperatures in Fahrenheit: Formula : F = 1.8 *C + 32.0 Functional notation : F = celsisus2Fahrenheit(C) where celsius2Fahrenheit(C) = 1.8*C + 32.0
Function definition Math: f(C ) = 1.8*C + 32.0 Python: Terminology : parameter “C”
© 2011 Pearson Addison-Wesley. All rights reserved.
Definition and calling
Triple quoted string in function A triple quoted string just after the def is called a docstring docstring is documentation of the function’s purpose, to be used by other tools to tell the user what the function is used for.
Abstraction
Abstraction
Abstraction The ability of dealing with ideas rather than events. Something that exists only as an idea.
Abstraction in computing A programmer would use abstraction to define general purpose functions. Abstraction is one of the most important techniques in software engineering as it is used to reduce complexity.
Reusability
Why have functions? Support divide-and-conquer strategy Abstraction of an operation Reuse: once written, use again Sharing : if tested, others can use Security : if well tested, then secure for reuse Simplify code : more readable
How to write a function Does one thing . If it does too many things, it should be broken down into multiple functions ( refactored ).Readable . You should be able to read it as well as others.Reusable . If it performs its task well , you can reuse.
More on functions Complete . A function should check for all the cases where it might be invoked. Check for potential errors. Not too long . As it does one thing, code is usually succinct.
Parameters and arguments
Parameters vs argument A parameter is the variable which is part of the function's definition. Inside the function, the arguments are assigned to the parameters. An argument is an expression, value, or literal used when calling the method.
© 2011 Pearson Addison-Wesley. All rights reserved. val=25* 1.8 + 32 = 77.0 celsius=25
Returning values
Return Statement The return statement indicates the value that is returned by the function. The return statement is optional. If there is no return statement, the function is often called a procedure . Procedures are often used to perform duties such as printing output or storing a file
Multiple returns in a function A function can have multiple return statements, but only one will execute. The first return statement found, executes and ends the function.Multiple return statements can make your code confusing, therefore should be used carefully.
Multiple return statements example
Variables scope
Local variables When you create a variable inside a function, it is local , which means that it only exists inside the function. For example: This function takes two arguments, concatenates them, and prints the result twice. Here is an example that uses it:
Local values When cat_twice terminates, the variable cat is destroyed. If we try to print it, we get an exception: Parameters are also local. For example, outside cat_twice , there is no such thing as part1 or part2 .
Summary Function : Named sequence of statements that performs a computation Built in functions : Python provides built-in functions and modules. Lots of extra modules available User defined functions : It is also possible to add new functions. A function definition specifies the name of a new function and the sequence of statements that execute when the function is called. Abstraction : The ability of dealing with ideas rather than objects or events.
Summary Reusability : Functions can be re-used in another program. Parameters and arguments : A parameter is the variable which is part of the function’s definition. An argument is the value or expression used when calling the method. Returning values : The return statement indicates the value returned by the function. A function with no return statements is often called a procedure . Variables Scope : Parameters and variables created inside a function are local , which means that they only exists inside the function
Status of Assignment and Quizzes Past Due RA1_ComputerConcepts RA2_PythonCh1Ch2 RA3_PythonCh5Ch7Quiz1 Pending RA4_PythonCh3Ch6 RA5_PythonCh8Ch10 Labs Lab1 Lab2 Lab3 Lab4