DEV Community

kster
kster

Posted on • Updated on

Python Functions

Functions are a convenient way to group our code into reusable blocks.

In Python defining a function looks something like this.

def function_name():
    #function tasks go here 
Enter fullscreen mode Exit fullscreen mode
  • def keyword indicates the beginning of a function.
  • Following the function name is parenthesis ( ) that can hold input values.
  • A colon : marks the end of the function
  • Similar to loops and conditions in python, code inside a function must be indented to indicate that they are part of the function.

Calling a function

Now, to call a function you must type out the functions name followed by ( ) parentheses. This does not need to me indented.

function_name()
Enter fullscreen mode Exit fullscreen mode

Parameters & Arguments

Function Parameters allow our function to take in data as an input value.

Heres an example

def welcome_guest(name):
  print("Welcome to our Airbnb!")
  print("We are excited to have you come stay with us" + name)
Enter fullscreen mode Exit fullscreen mode

We would use our parameter by passing an argument to the function like this

welcome_guest("Sam")
Enter fullscreen mode Exit fullscreen mode

This would output:

Welcome to our Airbnb!
We are excited to have you come stay with us Sam.
Enter fullscreen mode Exit fullscreen mode

Here is a visualization of the distinction between a parameter and an argument

This is a diagram representing a distinction between a parameter and an argument

Top comments (0)