Functions in Python: Difference between revisions

From Computer Science Wiki
No edit summary
Line 31: Line 31:
==Another classic example of a function==
==Another classic example of a function==


<syntaxhighlight lang="python" line="1" >
<syntaxhighlight lang="python">
#
#
# this is a simple function
# this is a simple function
Line 54: Line 54:
<iframe width="560" height="315" src="https://www.youtube.com/embed/qO4ZN5uZSVg" frameborder="0" allowfullscreen></iframe>
<iframe width="560" height="315" src="https://www.youtube.com/embed/qO4ZN5uZSVg" frameborder="0" allowfullscreen></iframe>
</html>
</html>
== Do you understand functions? ==
=== Beginning level functions ===
# Write a function which accept a string and returns the string in uppercase
# Write a function which accepts a string and returns the string capitalized
# Write a function that accepts 2 numbers and returns the sum
# Write a function that accepts 2 numbers and returns the quotient
# Write a function that accepts 2 numbers and returns the product
# Write a function that accepts 2 numbers and returns the difference
=== Average level difficulty ===
# Write a function that accepts two arguments. A name (as string) and a nice greeting (as a string). Your function should return a full sentence.
for example my_function("joe","happy birthday")
returns -> Joe, I'd like to wish you a very happy birthday!
=== Expert level difficulty ===
</syntaxhighlight>


==References==
==References==

Revision as of 20:39, 10 April 2016

A function f takes an input x, and returns a single output f(x). One metaphor describes the function as a "machine" or "black box" that for each input returns a corresponding output.[1]

Introduction

In programming, a named section of a program that performs a specific task. In this sense, a function is a type of procedure or routine. Some programming languages make a distinction between a function, which returns a value, and a procedure, which performs some operation but does not return a value.

Most programming languages come with a prewritten set of functions that are kept in a library. You can also write your own functions to perform specialized tasks. [2]

We use function so we don't need to repeat ourselves.

Example of a function

#
# this is a simple function
#

def welcome(name):
    if name == "Alisher":
        likes_hamburgers ="yes"
    else:
        likes_hamburgers="no"
    return likes_hamburgers
    
print welcome("Bill")
print welcome("Alisher")    
print welcome("foo")


Another classic example of a function

#
# this is a simple function
#

def calculator(number1, number2):
    answer = number1 + number2
    return answer
    
print calculator(12,43)
print calculator(91,673)
print calculator(1,3)
print calculator(87,1098)

Some decent videos about functions in Python

In the video below, the programmer uses a different IDE than we do (we use canopy). The programmer also uses tests in this video, which we haven't covered yet. The video on the right is pretty good example of what a function is, and also discusses variable scope

Do you understand functions?

Beginning level functions

  1. Write a function which accept a string and returns the string in uppercase
  2. Write a function which accepts a string and returns the string capitalized
  3. Write a function that accepts 2 numbers and returns the sum
  4. Write a function that accepts 2 numbers and returns the quotient
  5. Write a function that accepts 2 numbers and returns the product
  6. Write a function that accepts 2 numbers and returns the difference

Average level difficulty

  1. Write a function that accepts two arguments. A name (as string) and a nice greeting (as a string). Your function should return a full sentence.
for example my_function("joe","happy birthday")
returns -> Joe, I'd like to wish you a very happy birthday!

Expert level difficulty

</syntaxhighlight>

References