Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman.

Slides:



Advertisements
Similar presentations
Recursion CS 367 – Introduction to Data Structures.
Advertisements

Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Q and A for Chapter 6 CS 104 Victor Norman. Return values Q: A function definition that returns a value (i.e., a non-void function) must have at least.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Lists Introduction to Computing Science and Programming I.
Pointers “Absolute C++” Section 10.1
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Python Programming Chapter 3: Functions Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
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.
FUNCTIONS. Function call: >>> type(32) The name of the function is type. The expression in parentheses is called the argument of the function. Built-in.
Python – Part 3 Functions 1. Function Calls Function – A named sequence of statements that performs a computation – Name – Sequence of statements “call”
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Variable Scope Storage Class Recursion
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Functions CS 103 March 3, Review A function is a set of code we can execute on command to perform a specific task A function is a set of code we.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 5.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
Peyman Dodangeh Sharif University of Technology Fall 2013.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Python Functions.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Python Functions : chapter 3
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,
Python Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
PYTHON FUNCTIONS. What you should already know Example: f(x) = 2 * x f(3) = 6 f(3)  using f() 3 is the x input parameter 6 is the output of f(3)
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
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.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
A Lecture for the c++ Course
Functions.
Advanced Programming Behnam Hatami Fall 2017.
More Object Oriented Programming
Teaching London Computing
Python 17 Mr. Husch.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Passing Parameters by value
CS 1111 Introduction to Programming Fall 2018
CS 1111 Introduction to Programming Fall 2018
Nate Brunelle Today: Functions again, Scope
Writing Functions.
Python 17 Mr. Husch.
Recursion Taken from notes by Dr. Neil Moore
Nate Brunelle Today: Functions
CISC101 Reminders Assignment 3 due today.
Nate Brunelle Today: Functions
Nate Brunelle Today: Functions again, Scope
def-ining a function A function as an execution control structure
Corresponds with Chapter 5
Methods and Data Passing
Week 7 - Friday CS 113.
Presentation transcript:

Q and A for Chapter 3.4 – 3.14 CS 104 Victor Norman

Creating new functions Q: Can you create new functions with new names, or just use the one they show in the book? A: You can create any function you want with any legal name (legal identifier, like for variables). You must define the function before calling it.

Creating new functions def aFunc(param1, param2): # code indented 4 spaces that does stuff print param1 + param2 def yell(param1): # param1 must be str print param1.upper() aFunc(3, 4) aFunc(“Hello”, “world”) hi = “Greetings, earthling.” yell(hi)

String functions Q: Why create functions that work with strings? Functions to compute math stuff are obvious. A: Lots of computing is with strings. def prependTime(outStr): # code here to get the time print time + “: “ + outStr

Function Parameters Q: Can functions take more than 1 or 2 parameters? A: Yes: a function definition specifies how many parameters it takes. The function call must provide that many arguments. (Reminder: a function call evaluates its arguments before calling the function, where the parameters point to those values within the function code.)

Functions 2 Q: Can you define a function inside a function? A: You can, but you better not until you’ve been working with python for a few years. And, besides, that inner function definition goes away when the outer function call returns, so it isn’t very useful.

Stack frames! Help! Q: I need help with this stack frames business. Does python actually do this? A: Python does this internally, to keep track of what code is calling what code, etc. You don’t see this when it is running. Check this out.this

Local Variables Q: I don’t understand local variables. A: (That’s not a question… :-) A local variable in a function is a name that refers to a value, but is created when the function code is run, and is deleted when the function returns. (A parameter is a local variable that is initialized to refer to the value passed in by the corresponding argument.)

References to functions Q: What happens when you call a function without ()? Like this: supposed print_twice was defined, but you “call” it this way: print_twice (without the ()) A: This is just a reference to the function print_twice. It is a reference to where the function is defined in memory. It is not a function call.

void functions? Q: What is the point of a void function that doesn’t return something? A: Functions that don’t return things usually: print something, or, alter some object that is passed in. So, they can be very useful.

from someModule import * Q: Why not always just import everything from a module? So, that you don’t have to do module.function or module.variable every time? A: You will pollute the namespace by doing it this way. But, that’s OK… for now… (We’ll talk about this much much later.)