Calling functions [Built-In]

--Originally published at Quirino´s Projects

monty-pythone28099s-search-for-the-holy-grail-tim

You get to know the power of programming get you get to functions, functions are a very useful way to reuse code, or a easy way to save a few lines in your code


#In python3 even print is a function

print("Hello World")

A function takes an argument, or arguments and work with it.

#To call a function you just need to write its name
#And give enough arguments and the correct type
#print takes strings, variables
hellow = "World"
print ("Hello") #prints "Hello"
print (hellow) #prints "World"
#Functions like print work with different values, but there are some

#That need specific types like:

print (abs("Hola")) #Would cause error since it needs an int or float
print (int(["Hello",42])) #Error since int() doesn't take lists

 

#Lets review the sum() function
#sum() takes an iterable object and returns it sum. Example
moneys = [500,700,-330,24,-30]
print (sum(moneys))
#Where sum() is the function itself, and moneys its the argument
#this would print 864

This function comes included with with python, this means you don’t have to define it as you would with a user created like

#We will get in deep about user defined functions in "Creating functions"
def doubleSum(x,y):
#This funcion takes 2 numbers and returns its double
    return 2 * (x + y)
print doubleSum(5,6)

 

External Links:

[Really useful]

https://docs.python.org/3/library/functions.html