Python Functions Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Purpose of functions Code reuse A function can be used several.

Slides:



Advertisements
Similar presentations
Lilian Blot PART V: FUNCTIONS Core elements Autumn 2013 TPOP 1.
Advertisements

Lilian Blot CORE ELEMENTS PART V: FUNCTIONS PARAMETERS & VARIABLES SCOPE Lecture 5 Autumn 2014 TPOP 1.
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Guide to Programming with Python
Chapter 5 Functions.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
CHAPTER 6 Functions. Function Overview We’ve used built-in functions:  Examples:  print(“ABC”, x+10, sep=“:”)  round(x * 5, 2)  pygame.draw.circle(screen,
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Local Definitions and Scope. Definitions We've seen global definitions: (define answer 42) ;; "42" is RHS (define (f x) (+ 1 x)) Today we'll look at local.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Chapter 6 Functions 1. Opening Problem 2 Find the sum of integers from 1 to 10, from 20 to 37, and from 35 to 49, respectively.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Chapter Function Basics CSC1310 Fall Function function (subroutine, procedure)a set of statements different inputs (parameters) outputs In.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Functions Chapter 6. Function Overview We’ve used built-in functions: – Examples: print(“ABC”, x+10, sep=“:”) round(x * 5, 2) pygame.draw.circle(screen,
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
Chapter 14 Advanced Function Topics CSC1310 Fall 2009.
Python Functions.
Function Basics. Function In this chapter, we will move on to explore a set of additional statements that create functions of our own function (subroutine,
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
6. FUNCTIONS Rocky K. C. Chang October 4, 2015 (Adapted from John Zelle’s slides)
Python Sets and Dictionaries Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Set properties What is a set? A set is a mutable.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 5: Functions – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS 1110 Introduction to Programming Spring 2017
Chapter 6 Functions.
Topic: Functions – Part 2
JavaScript: Functions
Fundamentals of Programming I Managing the Namespace
Function There are two types of Function User Defined Function
Functions CIS 40 – Introduction to Programming in Python
Functions.
Python Classes By Craig Pennell.
CHAPTER FOUR Functions.
Writing Functions( ) (Part 5)
Starting Out with Programming Logic & Design
Chapter 3 Simple Functions
Functions, Procedures, and Abstraction
Chapter 4 void Functions
Passing Parameters by value
Chapter 7: Using Functions, Subs, and Modules
5. Functions Rocky K. C. Chang 30 October 2018
Chapter 9: Value-Returning Functions
G. Pullaiah College of Engineering and Technology
Topics Introduction to Functions Defining and Calling a Function
Function.
A function is a group of statements that exist within a program for the purpose of performing a specific task. We can use functions to divide and conquer.
Starting Out with Programming Logic & Design
CS 1111 Introduction to Programming Spring 2019
Predefined Functions Revisited
The structure of programming
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Thinking procedurally
Function.
Functions, Procedures, and Abstraction
Parameters and Arguments
Functions and Abstraction
Presentation transcript:

Python Functions Peter Wad Sackett

2DTU Systems Biology, Technical University of Denmark Purpose of functions Code reuse A function can be used several times in a program Can be used in several programs Minimize redundancy Generalize to enhance utility Hide complexity Will allow a higher view of the code Removes unimportant details from sight Allows ”Divide and Conquer” strategy – sub tasks

3DTU Systems Biology, Technical University of Denmark Function declaration def funcname(arg1, arg2, arg3, … argN): ”””Optional but recommended Docstring””” statements return value The name of the function follows the same rules as variables. There can be any number of complex python statements, if’s, loops, other function calls or even new function definitions in the statements that make up the function. The special statement return returns the computed value of the function to the caller There can be any number of return’s in the function body. A return with no value gives None to the caller. Any object can be returned. If the function ends with no return, None is returned

4DTU Systems Biology, Technical University of Denmark Function arguments 1 def funcname(arg1, arg2, arg3, … argN): The arguments can be positional or keyword def subtract(firstnumber, secondnumber): return firstnumber-secondnumber Use as result = subtract(6, 4) result = subtract(secondnumber=6, firstnumber=11) Other forms of argument passing exists.

5DTU Systems Biology, Technical University of Denmark Function arguments 2 def funcname(arg1, arg2, arg3, … argN): The arguments are assigned to local variables. Any assignment to the arguments inside the function will not affect the value from the caller. def change(number): number += 5 mynumber = 6 change(mynumber) print(mynumber)# mynumber is still 6 However if an argument is mutable, like a list, dict or set, then changes made to the argument is seen by the caller. def addtolist(alist): alist.append(5) mylist = [1, 2, 4] addtolist(mylist) print(mylist)# mylist is now [1, 2, 4, 5]

6DTU Systems Biology, Technical University of Denmark Function Docstring def funcname(arg1, arg2, arg3, … argN): ”””The Docstring””” The Docstring is the first ”statement” of a function. It describes the function. Documentation. It uses triple quotes for easy recognition and change. Further documentation of Docstring

7DTU Systems Biology, Technical University of Denmark Function scope def funcname(arg1, arg2, arg3, … argN): IamLocal = 4 NobodyKnowsMeOutside = ’unknown’ global KnownOutside KnownOutside = 7# Global change Any variables assigned to inside a function are local to the function. No namespace clash. A global variable can be used inside a function, if no assignment is made to it, or it is declared with the global keyword. It is usually an error to make functions that uses the global keyword.

8DTU Systems Biology, Technical University of Denmark Function recursion A function can call itself - recursion def print_numbers(number) if number > 1: print_numbers(number -1) print(number) print_numbers(10) There must be way way to stop the recursion or we have a never ending loop.