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 ):
function_suite
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

Loading...

We Are Currently Working On Our Android Application Development and We already have two YouTube channels their name is CodeX or NightHawk. CodeX is for programming related videos and NightHawk is for online game streaming so please supporting us and Subscribe to our channels, Thank you