![]() |
|
[2.7] Introduction to functions in python - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Python (https://sinister.li/Forum-Python) +--- Thread: [2.7] Introduction to functions in python (/Thread-2-7-Introduction-to-functions-in-python) Pages:
1
2
|
[2.7] Introduction to functions in python - Harvey - 04-10-2013 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
instructionsThat'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? RE: Introduction to functions in python - Greed - 04-10-2013 Ah thanks, This is what im looking for:p But you didnt answered my PM
RE: Introduction to functions in python - Harvey - 04-10-2013 (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 RE: Introduction to functions in python - Greed - 04-10-2013 (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 I sent you another one:p RE: Introduction to functions in python - Redbull - 04-10-2013 Thanks for this Harvey, this helped me out a bit. RE: Introduction to functions in python - Harvey - 04-11-2013 (04-10-2013, 07:46 PM)Redbull Wrote: Thanks for this Harvey, this helped me out a bit. No worries, glad you liked it.
RE: Introduction to functions in python - Clique - 04-11-2013 Awesome guide on functions. I'm actually getting into python now -- thanks! RE: Introduction to functions in python - Redbull - 04-11-2013 Harvey, i well be releasing my tutorials pretty soon for VB
RE: Introduction to functions in python - Harvey - 04-12-2013 (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 RE: Introduction to functions in python - Clique - 04-12-2013 I've been using http://www.codecademy.com/tracks/python to guide me along the way also. Have you used them? |