Functions in Python: Difference between revisions
Line 79: | Line 79: | ||
returns -> Joe, I'd like to wish you a very happy birthday! | returns -> Joe, I'd like to wish you a very happy birthday! | ||
# write a function that returns an ascii-art number ([http://patorjk.com/software/taag/#p=testall&f=Graffiti&t=1234567890 please click here to get the ascii numbers]) | |||
for example: print my_number(132) | for example: print my_number(132) | ||
would return: | would return: |
Revision as of 07:46, 11 April 2016
Introduction[edit]
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.
The difference between returning and printing[edit]
Students often print from within a function. Please understand the difference between printing a result from a function and returning a result from a function.
print
: gives the value to the user as an output string. print(3) would give a string '3' to the screen for the user to view. The program would lose the value.
return
: gives the value to the program. Callers of the function then have the actual data and data type (bool, int, etc...) return 3 would have the value 3 put in place of where the function was called.[3]
Example of a function[edit]
#
# 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[edit]
#
# 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[edit]
In the video to the left, 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?[edit]
Beginning level functions[edit]
- 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[edit]
- 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 print my_function("joe","happy birthday") returns -> Joe, I'd like to wish you a very happy birthday!
- write a function that returns an ascii-art number (please click here to get the ascii numbers)
for example: print my_number(132)
would return:
.--,-``-. ,---, ,----, / / '. ,`--.' | .' .' \/ ../ ; / / : ,----,' \ ``\ .`- ' : |.' ' | : . ;\___\/ \ : `----': | ; |.' / \ : | ' ' ; `----'/ ; / / / | | | / ; / \ \ \ ' : ; ; / /-, ___ / : | | | ' / / /.`| / /\ / : ' : |./__; : / ,,/ ',- . ; |.'| : .' \ \ ; '---' ; | .' \ \ .' `---' `--`-,,-'
3. Write a function that takes a string and returns the letters shifted 2 spaces to the right.
for example: encode("abcdefg") would return: cdefghi
Expert level difficulty[edit]
References[edit]
A possible solution[edit]
Click the expand link to see one possible solution, but NOT before you have tried and failed at least three times
not yet :-)