Hi, I can't speak about python as I am now discovering it my self but I do understand a bit about functions. So let me try to explain as a newbie to newbie. The examples below are not valid python code but they do display the logic.

Lets say you want two temperatures in Fahrenheit but you have two temperatures in Celsius instead. The formula to convert the temperature from Celsius to Fahrenheit is x * 1.8 + 32 = y where x is the temperature in C and y is the temperature in F. So now we have

var x1 = 30 #first variable in C
var x2 = 50 #second variable in C

and if you want the temperatures in F
var y1 = x1 / 1.8 + 32
var y2 = x2 * 1,8 + 32

For such a small code this works well. It is not a big deal. What if you have a complicated mathematical formula in there or many things to do? It will get quite lengthy and tiresome to type the same things again and again. Also are you sure you can type it always without errors? I purposely inserted a typo in both calls. Did you catch them? What if they were 30 calls like this?

Introducing functions. A function is a programming ummm... thing that can take few parameters and perform an action and optionally return a result. Following the example above we can introduce this function

celsiusToFahrenheit(temperatureInCelsius=0)
    var temperatureInFahrenheit = temperatureInCelsius * 1.8 + 32
    return temperatureInFahrenheit

Above *celsiusToFahrenheit* is the name of the function.
*temperatureInCelsius* is the parameter. There can be more than one.

( For example a function that adds 2 numbers could be

add(number1, number2)
    return number1 + number2
)

Notice that it is defined as temperatureInCelsius=0. That means that in case you don't provide any parameters it will use 0 as default. During the workings of the function that parameter can act as a variable.


As soon as the function finishes it's job that variable and any other variables defined (e.g. temperatureInFahrenheit) will be destroyed and cease to exist. Only the *value that is returned* survives and you must assign it to a variable in your main program otherwise you will loose that too.

*return* is what it will return back where you called it. You may write a function that doesn't need to return anything. In that case you can omit the return statement.

Now our code above could be written as


celsiusToFahrenheit(temperatureInCelsius=0)
    var temperatureInFahrenheit = temperatureInCelsius * 1.8 - 32
    return temperatureInFahrenheit

var x1 = 30 #first variable in C
var x2 = 50 #second variable in C

and if you want the temperatures in F
var y1 = celsiusToFahrenheit(x1)
var y2 = celsiusToFahrenheit(x2)
var y3 = celsiusToFahrenheit()
celsiusToFahrenheit(x1) # kiss the returned variable bye bye

The first two y1 and y2 will call celsiusToFahrenheit with x1 or x2 as parameter. Notice that we don't need to use the same name for the parameter in the declaration and in the function call. The third variable y3 calls it without parameters. This is possible because in the definition we have temperatureInCelsius=0 so the parameter is substituted automatically to 0 and that function will return 32. BTW in that function I have made another typo. Did you catch it? In how many places do you have to fix the typo now?

I hope this helps.














On 04/07/2014 07:02 AM, keith papa wrote:> Hi my name is keith and am new to python programming, Am learning python
> for the first time with the help of coursera problem is am starting a
> new topic call functions in python and am totally lost. can you please
> help me understand how function work? why it use in python, what are the
> rules of function etc.
>
>
> this some of the code the teacher used as an example.
>
>   http://www.codeskulptor.org/#examples-functions.py
>
>
> this is the video explaing functions:
>
> https://onedrive.live.com/redir?resid=B91E3E58ABADC574%21863
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>





On 04/07/2014 07:02 AM, keith papa wrote:
Hi my name is keith and am new to python programming, Am learning python
for the first time with the help of coursera problem is am starting a
new topic call functions in python and am totally lost. can you please
help me understand how function work? why it use in python, what are the
rules of function etc.


this some of the code the teacher used as an example.

  http://www.codeskulptor.org/#examples-functions.py


this is the video explaing functions:

https://onedrive.live.com/redir?resid=B91E3E58ABADC574%21863


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to