Functions in Programming - Basics and Mistakes to Avoid

 
Functions
 
Victor Norman
CS104
Calvin College
 
Reading Quiz
 
Counts toward your grade.
Function Def’n vs. Call
 
Definition:
def funcname(param_names):
 
statements to execute when funcname is called
Call:
y = int(input(“Enter your age: “))
result = funcname(y)
How many function calls in this code?
Call must provide the same # of values as
the function definition requires.
Function Defn is an Abstraction
 
A function 
abstracts
 and 
names
 lines of code.
can be called repeatedly
can take arguments.  Consider: y = sin(x).
Takes a value x, runs the sin function on it, producing a
result that y is made to refer to.
Non-fruitful Functions
 
Non-fruitful functions produce no value for
the caller.
Just execute their statements
Not useful unless they do something like print
something or make the robot move, etc.
Useful for turtle graphics.  E.g.,
def drawSquare(size):
code here to draw a square of size.  Returns
nothing.
Fruitful Function
 
Computes a value and returns it to the caller.
def squared(val):
    newval = val * val
    return newval
Return sends value back to the caller and
exits 
the code in the function.
Function will exit the code anyway at the end
of the function.
 
Mistakes CS104ers Make
 
Provide literals as parameters in function
definition:
def xyz(p1, “hello”):
    # code here
Ignoring the parameter value.
def xyz(p1):
    p1 = 3
    # code here using p1
 
Socrative Question
 
Given this code:
1  def foo(x):
2
   return x + 1
3
def bar(x, y):
4
   t = foo(x)
5
   return t + y
6
def baz(z):
7
   t = bar(z, z)
8
   return foo(t)
9
n = 3
10
res = baz(n)
11
print(res)
Parameters and Local Variables
 
Parameters hold the *values* being passed in
in the function call.
Both are defined only within the code of the
function.
That is their 
scope
 or 
lifetime
.
They can be altered within the code.
Can think of them as "temporary variables" --
places to hold values while you work on
computing the result or doing the work.
Return Statement
 
Computes the result to be sent back to the
caller.
Exits the code in the function, even if there is
code after it.
E.g.,
  
def computeIt(x, y, z):
    w = x + y / z
    return w
    print("Done!") # never executed
Return vs. print()
 
Printing a value in a function does 
not
 return
it.
print sends characters out to the output “stream”
– i.e., the screen.
To send a value back to the caller you must
use 
return <val>
 
Socrative Question
 
Given this code:
1 
def beatHope(i):
2
   i = i * i
3
   print("Let's beat Hope", i, "times")
4
print(beatHope(3))
 
Assignment
 
Do ~21 questions from the Functions section
in CodeLab for Saturday, 11:59:59 pm.
Study for test on Thursday.
Slide Note
Embed
Share

Explore the fundamentals of functions in programming, including definitions, calls, types (fruitful vs. non-fruitful), parameters, local variables, and common mistakes to watch out for. Discover the importance of functions in organizing and optimizing code, and learn key concepts through examples and explanations.

  • Functions
  • Programming Basics
  • Mistakes
  • Parameters
  • Local Variables

Uploaded on Sep 29, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Functions Victor Norman CS104 Calvin College

  2. Reading Quiz Counts toward your grade.

  3. Function Defn vs. Call Definition: def funcname(param_names): statements to execute when funcname is called Call: y = int(input( Enter your age: )) result = funcname(y) How many function calls in this code? Call must provide the same # of values as the function definition requires.

  4. Function Defn is an Abstraction A function abstracts and names lines of code. can be called repeatedly can take arguments. Consider: y = sin(x). Takes a value x, runs the sin function on it, producing a result that y is made to refer to.

  5. Non-fruitful Functions Non-fruitful functions produce no value for the caller. Just execute their statements Not useful unless they do something like print something or make the robot move, etc. Useful for turtle graphics. E.g., def drawSquare(size): code here to draw a square of size. Returns nothing.

  6. Fruitful Function Computes a value and returns it to the caller. def squared(val): newval = val * val return newval Return sends value back to the caller and exits the code in the function. Function will exit the code anyway at the end of the function.

  7. Mistakes CS104ers Make Provide literals as parameters in function definition: def xyz(p1, hello ): # code here Ignoring the parameter value. def xyz(p1): p1 = 3 # code here using p1

  8. Socrative Question Given this code: 1 def foo(x): 2 return x + 1 3 def bar(x, y): 4 t = foo(x) 5 return t + y 6 def baz(z): 7 t = bar(z, z) 8 return foo(t) 9 n = 3 10 res = baz(n) 11 print(res)

  9. Parameters and Local Variables Parameters hold the *values* being passed in in the function call. Both are defined only within the code of the function. That is their scope or lifetime. They can be altered within the code. Can think of them as "temporary variables" -- places to hold values while you work on computing the result or doing the work.

  10. Return Statement Computes the result to be sent back to the caller. Exits the code in the function, even if there is code after it. E.g., def computeIt(x, y, z): w = x + y / z return w print("Done!") # never executed

  11. Return vs. print() Printing a value in a function does not return it. print sends characters out to the output stream i.e., the screen. To send a value back to the caller you must use return <val>

  12. Socrative Question Given this code: 1 def beatHope(i): 2 i = i * i 3 print("Let's beat Hope", i, "times") 4 print(beatHope(3))

  13. Assignment Do ~21 questions from the Functions section in CodeLab for Saturday, 11:59:59 pm. Study for test on Thursday.

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#