0% found this document useful (0 votes)
27 views

Lesson 19 - Functions in Python: Quiz 4 Review

The document discusses functions in Python. It defines a function as a named group of instructions that performs a specific task. A function can be called multiple times in a program. When defining a function, the syntax uses the "def" keyword followed by the function name and parameters in parentheses. The body of the function is indented below the header. Functions allow breaking programs into reusable pieces of code to perform tasks.

Uploaded by

raasiboi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lesson 19 - Functions in Python: Quiz 4 Review

The document discusses functions in Python. It defines a function as a named group of instructions that performs a specific task. A function can be called multiple times in a program. When defining a function, the syntax uses the "def" keyword followed by the function name and parameters in parentheses. The body of the function is indented below the header. Functions allow breaking programs into reusable pieces of code to perform tasks.

Uploaded by

raasiboi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lesson 19 Functions in Python

Quiz 4 Review
WHILE LOOPS
Class_grades = [ [85,91,89] , [78,81,86], [62,75,77],]
Total = 0
K=0
While k < len(class_grades)
Total = total + class_grades[k][2]
K=k+1
Print(Avg. for Third exam=, total / float(k))

WITH FOR LOOPS


For k in class_grades:
Total = total + k[2]
Print(avg. for third exam = ,
Total / float (len(class_grades)))

AVERAGE OF ALL TESTS


Total = 0
For k in class_grades:
Total = total + sum(k)
Print( float(total) / (len(class_grades)*3))

Welcome to Functions
What is a Function?
-

Standalone program
Performs a known task
We might not even know how!
Print, input, len, key, etc.

When programs get really big, we count code lines!

Term
KLOC
MLOC
GLOC

Number of lines of code


1000
1000000
1000000000000

Equivalent storage
application

What is a function (AKA Routine)


A routine is a named group of instructions performing some task
A routine can be invoked (called) as many times as needed in a given program
When a function terminates, execution automatically returns to the point from which
it was called

Defining Syntax
Function header def avg(n1,n2,n3):
Function Body(suite) ----------------------------A function header starts with the keyword def
Followed by an identifier(avg)
which is the functions name
Function name followed by a comma-separated list of identifiers (n1,n2,n3)
Called formal parameters
Or simply parameters
Even an empty list of parameters, like? Exit, break, print
Then a colon ( : )
Def max(a,b):
If a>b:
Return a
Else:
Return b

How do we use it in a program?


X = int(input(Number 1))
Y = int(input(Number 2))
Print(Larger number is, max(x,y))

Lets RECAP
What a function entails
3 things!
Every Function
1) Performs a known task
2) Known inputs
3) Known return(s) or None

You might also like