Functions can be defined to execute specific tasks and can be called when ever needed.
The variables used in a function can be passed as arguments. When you want a specific task to be done multiple times at different points of time, functions come in handy.
← GO BACK : Python basics: Conditionals & Loops
FUNCTIONS
In the below example, we will create an array called ‘classroom’ that contains a few student names.
We will create a function called ‘enroll’ that help us enroll new students to the ‘classroom’.
The function takes the name of the student as an argument, to enroll.
#!/usr/bin/python #Create an array called classroom classroom=["student1", "student2", "student3", "student4"] #Print student list print "Current students list:" print classroom #Define function enroll def enroll(new_student ): print "Enrolling Student",new_student classroom.append(new_student) print classroom return; #Request student name. Exit when 'exit' is input student_name = raw_input("Enter name to enroll : ") while ( student_name != "exit" ): enroll(student_name) student_name = raw_input("Enter name to enroll : ") continue
As you see in the above example, functions are defined a keyword ‘def’ followed by ‘function-name’ and arguments in braces. The end of the function is marked with ‘return;’. A function block again needs to be intended, to mark the start and end of a block with ‘return;’ in the end. When the function is called, it must be called with the arguments that it was defined to accept. example function_name(arguement).
Below is the output of the example:
Current students list: ['student1', 'student2', 'student3', 'student4'] Enter name to enroll : student5 Enrolling Student student5 ['student1', 'student2', 'student3', 'student4', 'student5'] Enter name to enroll : student6 Enrolling Student student6 ['student1', 'student2', 'student3', 'student4', 'student5', 'student6'] Enter name to enroll : student7 Enrolling Student student7 ['student1', 'student2', 'student3', 'student4', 'student5', 'student6', 'student7'] Enter name to enroll : student8 Enrolling Student student8 ['student1', 'student2', 'student3', 'student4', 'student5', 'student6', 'student7', 'student8'] Enter name to enroll : exit
Functions with Multiple arguements and Python dictionary
Functions can take multiple arguments.
Lets Create a function that will not only enroll the student but also records the age.
Instead of using an array to store values, we will create a dictionary in Python. Python dictionary is a set of keys value pairs.
ages={'student1' : '17', 'student2' : '14', 'student3' : '18', 'student4' : '21'}
Lets print all the key value pairs in the dictionary with a for loop.
for i in ages: print i,ages[i]
student3 18 student2 14 student1 17 student4 21
Keys can be called by name, example
print ages['student1']
17
Going back to creating a function, we will create one that records name and age as key value pairs in a python dictionary.
#!/usr/bin/python ages={'student1' : '17', 'student2' : '14', 'student3' : '18', 'student4' : '21'} print "Current students and their ages are as follows:" for i in ages: print i,ages[i] def enroll( new_student, age ): print "Enrolling Student",new_student,"age :",age ages.update({new_student:age}) for i in ages: print i,ages[i] return; student_name = raw_input("Please enter student name : ") age = raw_input("Please enter student age : ") while ( student_name != "exit" ): enroll(student_name,age) student_name = raw_input("Please enter student name : ") age = raw_input("Please enter student age : ") continue
As you see, the example is same as the previous one, however it also takes a second argument and makes use of it in the statements that are in the block.
Thus, when calling the function it should be called with both the arguments, function_name(argument1, argument2).
Current students and their ages are as follows: student3 18 student2 14 student1 17 student4 21 Please enter student name : student5 Please enter student age : 32 Enrolling Student student5 age : 32 student3 18 student2 14 student1 17 student5 32 student4 21 Please enter student name : student6 Please enter student age : 12 Enrolling Student student6 age : 12 student3 18 student2 14 student1 17 student6 12 student5 32 student4 21 Please enter student name : student7 Please enter student age : 21 Enrolling Student student7 age : 21 student3 18 student2 14 student1 17 student7 21 student6 12 student5 32 student4 21 Please enter student name : student8 Please enter student age : 23 Enrolling Student student8 age : 23 student8 23 student3 18 student2 14 student1 17 student7 21 student6 12 student5 32 student4 21 Please enter student name : exit Please enter student age :