Week_4_Functions_1

Week_4_Functions_1 - CMPT120 J. Ye Functions (I) ( Week 4 )...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPT120 J. Ye Functions (I) ( Week 4 ) What is a function? Calling functions Define your own functions The advantages of functions CMPT120 J. Ye 1 What is a Function? Functions 2 In computer programming, a function may also be called: routine subroutine method (in Java) procedure It is a pre-made, self-contained set of instructions that could be used in programs Python has many built-in (already defined) functions that we can use when we write programs. We have seen print , type , input , range , len , str , etc. (Did you notice all these built-in function names appear in purple color when used?)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CMPT120 J. Ye A function may have arguments (input to the function) and return value (output from the function) 3 What is a Function? (cont) Functions FUNCTION f arguments (input) return value (output) “Scarlett” Return values are often assigned to a variable , e.g.: length = len( ) argument (input to len) assigns return value which is 8 to the variable length The concept of functions in programming is similar to that in mathematics For both mathematical function f(x) & f(x) in programming : f is the function name; x is the argument passed into function f ; x is a variable representing any possible value that f can take Return value is the calculated function value using x Function f can take more than one arguments, e.g. f(x, y) f(x) = 2x and f(x) = 3x-1 are two different functions. If both functions appear in the same math problem, or computer program, you have to name the second function g to avoid confusion You can use variable x as the argument of both f and g even when f and g appear in the same math problem, or computer program 4 What is a Function? (cont) Functions
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/28/2011 for the course CMPT 125 taught by Professor Diana during the Spring '08 term at Simon Fraser.

Page1 / 8

Week_4_Functions_1 - CMPT120 J. Ye Functions (I) ( Week 4 )...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online