Python Functions

Python functions is a block of code that performs some action which executes only on calling. 

The function declares a comma-separated variable within parentheses called as “parameters.”

Syntax:

Below is the syntax to define a function using def keyword. Function may or may not have return type.

def <function_name>():
// Function definition

Example

def printText():
print("Test");

# execute or call printTest function
printText();

Function Parameters and Arguments:

The function declares a comma separated variable within parentheses called as parameters. There can be any number of parameters defined within the parenthesis as comma separated after the method name. 
Function parameter can have any user-defined name. In the below syntax, param1 and param2 are just examples.

Arguments are the comma separated variables passed within parentheses while executing a function.

Syntax:

def <function_name>(param1, param2):
// Function definition

Example:

# printName is function name
# firstName and lastName is function parameters
def printName(firstName, lastName):
print(firstName, lastName);

# Here, foo and bar are function arguments.
print("foo", "bar");
    
OUTPUT:
foo bar

Return Value from function:

The value can be return from function using return keyword. 

def printText(a):
return a+ 1;

# execute or call printTest function and return a + 1 as a value.
print(printText(3));
print(printText(9));
     
OUTPUT:
      4
      10

Default Parameter value

In function declaration, we can provide a default parameter value. If no value is passed in function arguments when calling the function, the code within it will use the default value defined in the function parameter.

def printText(a = 1):
return a+ 1;

# execute or call printTest function and return a + 1 as a value.
print(printText(3));
print(printText(9));
# No value passed an a argument. It will take 1 by default from function parameter.
print(printText());
     
OUTPUT:
4
10
2

Pass keyword

We can have function with not content and pass helps us to create an function with an empty body. putting pass in an empty function helps us to avoid getting an error on run time.

Without pass keyword:

declaring function with an empty body will giving an error.

def printText():
OUTPUT:
File "<string>", line 1
def printText():
^
SyntaxError: unexpected EOF while parsing

With pass keyword:

Declaring a function with and empty body having pass keyword will not give an error as it is not completely empty and having a pass keyword. 

pass keyword does nothing and stops us for getting a run time error.

def printText():
OUTPUT:

Keyword Arguments:

As part of keyword argument we can send argument name and value such as name = “test”.

Order of argument doesn’t matter as we are passing argument with key and value pair.

Example:

def printText(fname, lname):
print("first Name: ", fname)
# calling a function without key
printText("one", "two");
# keyword argument with key and value
printText(fname = "one", lname = "two");
# keyword argument with change in position of fname and lname
printText(lname = "two", fname = "one");
    
OUTPUT:
first Name: one
first Name: one
first Name: one

Arbitrary Arguments:

When we are not sure of number of arguments while calling function. 

Arbitrary argument is define as *args.

We can access Arbitrary arguments using index number. If value is not passed or as an argument at particular index will give an error as “IndexError: tuple index out of range”

Syntax:

printText(*params):

Example

def printText(*languages):
print(languages[0])
# calling a function with single argument
printText("Java");
# calling a function with multiple arguments
printText("Java", "HTML", "CSS");
      
OUTPUT:
     Java
     Java

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan