< Prev
Next >

# Python - Functions with Arguments

In our last tutorial, we have explained how to define simple functions with no arguments in Python i.e. functions with empty parenthesis () and hence such functions were not passed any values when they were called. In this tutorial, we are going to explain how to define a function with arguments, which are also called formal arguments and pass values to such function, when it is called.

For doing this, when defining a function, we will have to declare the name of a variable within the parenthesis(), to which a value will be passed, when this function is called. Let's see how it is done.

• ## Function with argument example

• In the upcoming example, we are going to create a function named square which calculates the square of each integer variable passed to it when this function is called.

``````# Python - Defining functions with formal arguments

# Defining a function named  square with a formal parameter named num
def square(num):
result = num*num;
return result;

a = 5

# Square() function is called
result = square(a)

print('Square of a is = ', result);``````

## Output-

````Square of a is = 25`
```

## Program Analysis

• Function definition
• ``````# Defining a function named  square with a formal parameter named num
def square(num):
result = num*num;
return result;``````

Looking at the function definition, when the square function is called, it receives a copy of the value of the variable, a, passed to it, this value is copied into the variable named num, declared in the function definition.

The variable a being passed to the function(while calling) is known as the actual argument, while the num variable declared in the function definition can also be referred as the formal argument of the square function.

Eventually, the square function calculates the square of the value passed to formal argument num and returns the result using the return statement.

• Calling the function
• ``````# Square() function is called
result = square(a)``````

We have called the square method by passing the value of a variable named a as an argument. The variable a has a value 5 and it can also be referred as "actual argument" of the square method, when it is called.

• ## Calling a function with actual arguments of different type

• We can even create a program with a function, which is called with arguments of different type i.e. actual arguments of different type, as long as the body of defined function can work with the actual arguments passed to it without throwing an error. Let's see how.

``````# Python - Multiple functions of actual arguments with different type

# square function is defined
def square(num) :
square_result = num*num
return square_result

a =10
f = 10.5

# square() function is called
print('Square of 10 is = ', square(10))

#square() function is called again with a floating-point value
print('Square of 10.5 is = ', square(10.5)) ``````

## Output-

``````Square of 10 is =  100
Square of 10.5 is =  110.256``````

In the above mentioned coding example, we have created a function square which calculates the square of a number value passed to it through actual arguments, be this number value an integer or a floating-point.

• ## Type of actual arguments passed to a function must comply with the function definition

• An error is reported, if we call a function with actual arguments of an incompatible type with the statements in the body of the defined function. Let's see how.

``````# Python - Calling a function with  actual argument of an incompatible type

# square function is defined
def square(num) :
square_result = num*num
return square_result

# square() function is called with string value, resulting in a compile error
print('Square of abc is = ', square('abc')) ``````

## Output-

``````Square of 10 is =  100
Square of 10.5 is =  110.25
Traceback (most recent call last):
File "C:/Users/RadheKrishna/Desktop/Python Programs/func6.py", line 27, in
print('Square of abc is = ', square('abc'))
File "C:/Users/RadheKrishna/Desktop/Python Programs/func6.py", line 7, in square
square_result = num*num
TypeError: can't multiply sequence by non-int of type 'str'``````

In the above mentioned coding example, we have called the square function with an incompatible string value, which has resulted in an error, because one of the statements in function definition throws an error when working with the string value passed to it.

• ## Functions with multiple arguments

• We could even create functions with multiple arguments. Let's see how to do this.

``````# Python - Defining functions with multiple arguments

# subtraction function is defined
def subtraction(a,b):
subtract = a-b
return subtract

a,b = 10,5

print('Result of 10 + 5 = ', result)

# subtraction() function is called
result = subtraction(a,b)

print('Result of 10 - 5 = ', result)``````

## Output-

``````Result of 10 + 5 = 15
Result of 10 + 5 = 5``````

• ## Functions with mixed arguments

• We could even create functions with formal arguments of mixed or different types. Let's see how we do this.

``````# Python - Defining function with mixed arguments types

# Defining a function named misc with mixed arguments
def misc(intg, st, fl):
print('integer value : ', intg)
print('character value : ', st)
print('floating-point value : ', fl)

# A variable to contain an integer value
i = 5

# A variable to contain a string value
string = 'hello'

# A variable to contain a floating-point value
f = 9.9

# misc() function is called
misc(i,string,f)``````

## Output-

``````integer value :  5
character value :  hello
floating-point value :  9.9``````