[2.7] Introduction to functions in python 04-10-2013, 12:33 PM
#1
Hi guys,
Quick little tutorial on functions and how they're done in python. Let's start by looking at the basic syntax of a function.
That's how it's done in python. Pretty basic. You use def and then the name you want to give to your function and in your brackets you put one or several 'parameters' which are just place holders for variables to be passed through at a later date.
So how do we use this function in a program? Let's look at the following basic python program.
Now, if you ignore the function for the time being and focus on the rest of the application. You can see we output a message using the print() function. Pretty easy. But after that, we print the value returned from our welcome_message() function with the argument of a simple welcome message as a string.
The final line in this program basically calls the welcome_message() function and passes through some text. In that function, all that we do is return it meaning that's the final result we get from using that function.
Any questions?
Quick little tutorial on functions and how they're done in python. Let's start by looking at the basic syntax of a function.
Code:
def function_name(parameters):
instructions
instructions
That's how it's done in python. Pretty basic. You use def and then the name you want to give to your function and in your brackets you put one or several 'parameters' which are just place holders for variables to be passed through at a later date.
So how do we use this function in a program? Let's look at the following basic python program.
Code:
def welcome_message(your_text):
return your_text
print 'Hello and welcome to my program, you will now see some text below!'
print welcome_message('this is a simple welcome message!')
Now, if you ignore the function for the time being and focus on the rest of the application. You can see we output a message using the print() function. Pretty easy. But after that, we print the value returned from our welcome_message() function with the argument of a simple welcome message as a string.
The final line in this program basically calls the welcome_message() function and passes through some text. In that function, all that we do is return it meaning that's the final result we get from using that function.
Any questions?
(This post was last modified: 04-10-2013, 12:36 PM by Edward.)