Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding Functions
#1
Hello
I'm new to python i have been taking the Codeacademy python course, I'm trying to understand what a function is and how it can properly be used.
I have been reading about them/watching videos on them but i just don't understand it, I'm not sure if it just the way people are explaining it or i'm just dumb, Anyways if anyone has some useful link's/advice about them it would be much appreciated.
Reply
#2
A function is a reusable routine that does the same thing for a set of variable inputs:
as an example, if you were building a simple calculator, you would need functions for add, subrtract ,
multiply and divide among others.
the add function might look like this:
>>> def add(a, b):
...     return a + b
...
>>> add(4, 5)
9
>>> add(2, 3)
5
>>> z = 2
>>> y = 9
>>> add(z, y)
11
>>>
hope this is helpful
Reply
#3
functions group together any series of executions. If you repeat those more than twice, then you need a function. Then if you end up writing it a 3rd or 4th time, you really only have 1 spot to change if you have to change something, versus 4 different spots without a function. Even more, sometimes hundreds.

In Larz example. He called add() 3 times. Lets say he wanted to change the code to add 1 to every addition like
>>> def add(a, b):
...     return a + b + 1
The rest of the code stays the same. You dont have to change it anywhere else. Whereas if you didnt have a function, he would of had to change it 3 other times.
Recommended Tutorials:
Reply
#4
You write a function when you have to repeat a block of code more than once. 
number = int(input("> "))
if number % 2 == 0:
    print("even")
else:
    print('odd')

if number % 2 == 0:
    print(number * 2)
else:
    print(number ** 2)
You can write a function to check if the number is even:
def is_even(num): # here you define a function
    if num % 2 == 0:
        return True
    else:
        return False

number = int(input("> "))

if is_even(number): # here you call the function and it returns True or False
    print('even')
else:
    print('odd')
Or:
even = is_even(number)
if even:
    print('even')
else:
    print('odd')
As you can see in the definition, the function can take a parameter ( 'num' ) which you can use inside the function. Also. the function returns True or False. It actually can take and return any Python object. If you don't specify the return object it returns None.

The functions are objects too like everything in Python and they can be passed as a parameter to another function. Or you could do something like this.

def say_hello(text):
    print('Hello', text)

def bye_bye(text):
    print("Bye bye,", text)

say = print # the print() Python built-in function

functions = {'world': say_hello, 'happiness': bye_bye} # here the values are function objects

word = input("A word please: ")
word = word.lower()
if word in functions:
    functions[word](word) # here functions[word] holds an function and we call it adding () with a parameter - 'word'
else:
    say('Unknown word!') 
Both 'print'  and 'say' are just pointers to a function and you call them adding () at the end of the name just like 'function[word]' is a pointer ( its value )  to a function.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Yea after reading all these comments I think I understand it now, thanks everyone for the help I really appreciate it. :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Improving my understanding of functions and methods menator01 2 2,091 Apr-24-2020, 06:26 AM
Last Post: menator01
  Need help understanding a couple of functions (encrypt,decrypt, int_to_bytes) xoani 0 1,967 Jun-09-2019, 03:25 PM
Last Post: xoani
  i have problems understanding 2 functions,look for help. Tony 2 2,596 Dec-05-2018, 12:35 PM
Last Post: Tony
  Help in understanding scope of dictionaries and lists passed to recursive functions barles 2 3,148 Aug-11-2018, 06:45 PM
Last Post: barles

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020