Functions in Python: Difference between revisions

From Computer Science Wiki
No edit summary
Line 29: Line 29:




==another classic example of a function==
==Another classic example of a function==


<syntaxhighlight lang="python" line="1" >
<syntaxhighlight lang="python" line="1" >
Line 46: Line 46:


</syntaxhighlight>
</syntaxhighlight>
==References==
==References==



Revision as of 23:44, 20 March 2016

This is basic programming knowledge [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)

References