Python Functions :

A Function :

C Language Functions | C++ Language Functions - A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.

Defining a Function :

In Python a function is defined using the def keyword. For Example : def my_function()

Syntax :

def functionname( parameters ):
return [expression]

Note : By default, parameters have a positional behavior and you need to inform them in the same order that they were defined.

Calling a Function :

To call a function, use the function name followed by parenthesis.

Example :

  1. def my_function():
  2. print("Hello from a function.")
  3. my_function()
  4. input()

Output :

python calling a function

Function Parameters :

Information can be passed to functions as parameter. Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

Example :

  1. def my_function(website):
  2. print("www." + website + ".org")
  3. my_function("techbaz")
  4. input()

Output :

python function with parameters

Default Parameter Value :

If we call the function without parameter, it uses the default value. The following example shows how to use a default parameter value :

Example :

  1. def my_function(name = "ayan"):
  2. print("My name is : " + name)
  3. my_function("apu")
  4. my_function()
  5. my_function("dev")
  6. my_function("symul")
  7. input()

Output :

python function with default parameter value

Next Update


Currently, we are working on other servers, but soon we update our website with new content. So, stay tuned we are back soon with new content.

Free Courses