It is currently 27 Feb 2021, 12:22

• Featuring Members' Works (Image Attachments) •
1: member
2: member
3: member
4: member
5: member

GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  Python-Fu Plug-ins | Scheme-Fu Scripts | GIMP
Python:  🡆 Lessons | Questions | Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

Functions/Methods in Python

Simple lessons to get your started in Python language
tim
GL Blogger

Functions/Methods in Python

Post#1 by tim » 09 Nov 2018, 22:26


This is probably one of the most exciting parts about any programming language, functions (methods).

In Python you declare (define) a function by using the the def keyword followed by function name then function parameters inside brackets () (comma separated) and then end the def statement with a colon (:) to indicate that your function will begin in the following line(s). And at the end of the function, you can have a return statement to return the value(s) to the caller so that whenever you call the function, you'll get value(s) back.

Let's see an example of a function that simply adds 10 to any number we give it

Code: Select all

def add_ten(num):
    return_value = num + 10
    return return_value
The above function we created is named add_ten, it accepts one parameter (num) and then it declares a variable called return_value and set that to be whatever the parameter num is with 10 added to it. Then the function returns the return_value.

Now, for example, if we wanted to call the add_ten function with 5 as input we'd use
add_ten(5)

Can you guess what this call will return? It'll return 15 (because 5 + 10 is 15).

Functions are good when you have piece of code that you'd like to reuse with different input parameters to get different outputs.

You can call other functions and inside a function. A function can even call itself (which would make it a recursive function). This ability to create/define new functions is what makes programming so dynamic.

What Python also allows you to do which sometimes can be really useful is the ability to set a variable to be a function such as
my_func = add_ten
Then now you can call my_func like as if you were calling add_ten like
my_func(10).

I don't know any other languages (I am sure they exist just not all that common though) that lets you set a variable to hold a function like this.

Play with what you've learned here.

Next lesson: User Inputs in Python Previous lesson: If... Else... in Python
aka Tin

         


GIMP/GL:  Tutorials | Tips | Questions | Interests | Script Requests
Recommendations:  Index | Top 10
Resources:  Python-Fu Plug-ins | Scheme-Fu Scripts | GIMP
Python:  🡆 Lessons | Questions | Exercises
The Arts:  GIMPed | GIMPed Photos | Other | Contests | Public Domain Images | Isolated Images | Blender
Inkscape
Social:  Say Hello | Anything Goes | Shares | FCSP

  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest