Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[2.7] Introduction to functions in python filter_list
Author
Message
[2.7] Introduction to functions in python #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.

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.)

Reply

RE: Introduction to functions in python #2
Ah thanks, This is what im looking for:p But you didnt answered my PM Biggrin

Reply

RE: Introduction to functions in python #3
(04-10-2013, 12:38 PM)Greed Wrote: Ah thanks, This is what im looking for:p But you didnt answered my PM :D

I sure did. :) And yeah, sorry about the code. It's a little messed up at the moment. x.x

Reply

RE: Introduction to functions in python #4
(04-10-2013, 12:39 PM)Harvey M. Wrote:
(04-10-2013, 12:38 PM)Greed Wrote: Ah thanks, This is what im looking for:p But you didnt answered my PM Biggrin

I sure did. Smile And yeah, sorry about the code. It's a little messed up at the moment. x.x

I sent you another one:p

Reply

RE: Introduction to functions in python #5
Thanks for this Harvey, this helped me out a bit.

Reply

RE: Introduction to functions in python #6
(04-10-2013, 07:46 PM)Redbull Wrote: Thanks for this Harvey, this helped me out a bit.

No worries, glad you liked it. Smile

Reply

RE: Introduction to functions in python #7
Awesome guide on functions. I'm actually getting into python now -- thanks!

Reply

RE: Introduction to functions in python #8
Harvey, i well be releasing my tutorials pretty soon for VB Smile

Reply

RE: Introduction to functions in python #9
(04-11-2013, 04:07 PM)Clique Wrote: Awesome guide on functions. I'm actually getting into python now -- thanks!

Glad you chose such a beastly language. :p

Reply

RE: Introduction to functions in python #10
I've been using http://www.codecademy.com/tracks/python to guide me along the way also.

Have you used them?
[Image: Vm3TtLV.png]

Reply







Users browsing this thread: 1 Guest(s)