Good To Know:


Oopsie!Either you knew everything or there was a problem in fetching my facts

Posted by : Unknown Saturday 31 August 2013

logo python
User defined functions!they are pretty much very important in any programming language,and basically programming is all about it.in this class lets see hoe to define a function in python

Lets try defining our  own function for addition of two numbers,

python add function


If you can just look into the image,basically a function in python has Function name,body of the function,and a return statement.

  • A function must start with the key word "def" which says python that we are about to define a function.
  • Then the name of the function followed by a set of parentheses inside which any number of parameters separated by a comma ( , )can be passed to the function.
  • and after the parentheses a colon,which indicate we are about to write what happens when the particular function is called.so,the head of our function would look like,
def name_of_function(parameter1,parameter2......) :



  • parameters of a function are simply those initial data items with which you want to call a function,for example,it we want our function to give us the product of two numbers,you should say it what are those numbers for which you need a product,unless you say it,it doesn't know them (see computer ain't dat smart ;) )
  • Now comes the body of the function it is where you write what happens when the function is called .in the above example,we asked python to add up the values of a and b,then store it in variable c,this small piece of code is the body of the function add()
  • And all the functions end with a return statement.it is used to return a memory location reference when called.a return statement indicates the end of the function.unlike other languages you need no deal with messed up parenthesis and brackets,def starts a function,return statement ends it,thats all!
  • Now after typing the return statement you might wanna hit the enter key two times in order to get the prompt again.now lets test our function,
  • To use a function you simply need to call it with its name and the required number of arguments or else you will end up with a type error
python type error
  • When there are no errors you would get the output


python calling function

Arguments Vs Parameters :

   Its always been a confusing scenario whether to use the word arguments or parameters,well atleast for me,so i figured out i would just tell you the basic difference between an argument and a parameter,
difference between arguments and parameters

  • If you can see in the above image in case 1,a and b are called the parameters of the function,and in case 2,the values 5,7 are called the arguments of the function.you can simply say that those things in the parenthesis while defining a function are called as parameters and things in brackets while calling up the function are called arguments.
  • And by the way, # is used to mark a single line comment ie., any thing in a line starting from # is ignored by the compiler,comments are used to make programs more readable,off course we can ignore them in such small codes,but when the code gets bigger,the are definitely of a great help
Lets just have a quick look on how to save and load code in shells,

  • The python code files are called as shells.to save a shell,first complete typing you code,then goto file -> click save -> give it a name and click save.python shells will be with an extension of " *.py "
saving python shell


save as .py

  • And to load a shell,you can go to the location of the shell,right click and select edit with idle,
edit shell in idle

  • Then in the shell window,goto run and click run module
run module-python



  • Once you load a shell into python window,you can now access all the functions defined in that shell.

Summary:



The general form of a function definition:


def function_name(parameters):
                       body
                       return statement

  • def: a keyword indicating a function definition
  • function_name: the function name
  • parameters:
    • the parameter(s) of the function, 0 or more and are separated by a comma
    • a parameter is a variable whose value will be supplied when the function is called
  • body:
    • 1 or more statements, often ending with a return statement



The general form of a function call:

function_name(arguments)


Leave a Reply

Subscribe to Posts | Subscribe to Comments

Translate

Blogger Tips and Tricks

Popular Post

Random post

- Copyright © smartify me -

- Administered by Krishna Chaitanya -
Google+