Python: Functions, Arguments and Return
-Functions-
In python, a Function is a block of code which will only run when it is called and you can also pass data know as parameters into a Function, and you can return data as well in the form of a result. The way of creating a function in Python is by using the keyword "def" which is what defines it like the following:
def my_function():
print("This is how you do a function")
To call a Function in Python you would use the Functions name followed by a parenthesis like the following:
Input: def my_function():
print("This is how you do a function!")
my_function()
Output: This is how you do a function!
-Argument-
In Python information can be passed through Functions as Arguments. Arguments are usually specified after the Functions name within the parentheses, and you can also have as many Arguments as you want you would just need to separate them with a comma. The following example below has a function with just one argument (flightsaber). When this function is called we will pass along a colour which will then be used inside the function to print what type of colour the blade is:
Input:
def my_function(flightsaber):
print(flightsaber + " Blade")
my_function("Yellow")
my_function("Blue")
my_function("White")
Output:
Yellow Blade
Blue Blade
White Blade
-Return-
In Python the Return keyword is used to exit a Function and to return a certain value, for example:
Input:
def myfunction():
return 5555+66
print(myfunction())
Output:
5621
As you can see the return function gave us the vale of 5555+66 which is 5621. Another affect of the Return keyword is that statements after the return line will not be executed when the code is run, for example:
Input:
def myfunction():
return 5555+66
print("Arc Trooper Fives, Order 66!")
print(myfunction())
Output:
5621
As you can see when I tried to print the statment "Arc Trooper Fives, Order 66!" it would not print since the print command was placed after the first thing you are trying to return using the return line.
Programming for Computer Animation and VFX TB1
Status | In development |
Category | Other |
Author | Aronne_Monk |
Leave a comment
Log in with itch.io to leave a comment.