Introduction to Python Functions!!

Functions are named blocks of code that are designed to do one specific code. Functions facilitate reusability of code. When you want to do something repeatedly you define a function and call it whenever you need it.

Built-In Functions

Python comes with several built-in functions that you can use to create wonderful programs. Here are a few but not limited to built-functions.

print() range() input() list()
next() round() max() tuple()
str() dic() sorted() type()
object() sum() set() eval()

Creating a function

In python a function is defined using the def keyword for example:

def greet():
    print("Hello World")

Calling a function

The above function definition is inert until the the function is triggered or called. when you call the function:

greet() # Hello World

Arguments

Arguments refer to the information that is passed into the function Arguments are specified after the function name, inside the parenthesis. You can add as many arguments as you want but you have to separate them with a comma. for example:

def greet(name):
    print( "Hello" +" "+ name)
greet("John") # Hello John

Positional Arguments

Because a function can have multiple parameters, a function call may need multiple arguments.You can use positional arguments which need the same order the parameters were written. This is how it works:

def greet(name , place):
    print( f" Hello {name} from {place}")
greet( John, Uganda) # Hello John from Uganda

Now imagine a situation when then name and the place are swapped when the function is called.

greet(Uganda, John) # Hello Uganda from John

And this quite doesn't make sense. This means that the first argument in the function call is used as the value of the first parameter and the second argument in the function call is used as the value of the second parameter. To avoid such such situations from happening when can specify both the arguments and parameters together with the values that they take. These are called Keyword arguments. Here the order of the arguments in the function call doesn't matter as long as the names of the parameters are correct.

Now we have:

greet(place ="Uganda", name="John") # Hello John from Uganda

Arguments and Parameters

A parameter is the variable listed inside the parentheses in the function definition. An argument is the value that is sent to the function when it is called.

Number of Arguments

By default, a function should be called with correct number of arguments. Should call a function with more or less arguments than it expects it will bring an error.

for example:

def greet(name, place):
  print("Hello" + " " + name +" "+ place)

greet("Jack", "Ghana") # Hello Jack from Ghana

If you try to call this function with 1 or 3 arguments

greet("Jack")

You get this error:

Traceback (most recent call last):
  File "demo_function_args_error.py", line 4, in <module>
    greet("Jack")
TypeError: greet() missing 1 required positional argument: 'place'

Default Value Parameter

When writing a function, you can define a default value for each parameter. If an argument for a parameter is provided in the function call, Python uses the argument value. If not, it uses the parameter’s default value. So when you define a default value for a parameter, you can exclude the corresponding argument you’d usually write in the function call.

def people(country = "Kenya"):
  print("I am from " + country)

people("Sweden")
people("Burundi")
people("Ghana")
people("Nigeria")

Return Values
To let a function return a value, use the return statement:
def sum(x):
  return 4 + x

print(sum(1)) # 5
print(sum(3)) # 7
print(sum(10)) # 14

In conclusion, we have covered:

  • what are functions
  • Built-in functions
  • how to define functions
  • calling functions
  • Arguments
  • the difference between arguments and parameters
  • Return values
  • Hope you enjoyed reading this article. Thank you for reading.

13