Unit 3
Unit 3
def my_function():
print("Hello from a function")
Function Call
• To call a function, use the function name
followed by parenthesis
my_function()
Parameters & Arguments
Function – return statement
• To let a function return a value, use the return
statement
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
Function Return Type
Function that does not return any value
def fn():
print('Hello World!')
print(type(fn()))
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
Empty function in C/C++/Java
// An empty function in C/C++/Java
void fun()
{
}
Empty function in Python
# Incorrect empty function in Python
def fun():
1 A copy of the value is passed into the function An address of value is passed into the
function
2 Changes made inside the function is limited to Changes made inside the function validate
the function only. The values of the actual outside of the function also. The values of
parameters do not change by changing the the actual parameters do change by
formal parameters. changing the formal parameters.
3 Actual and formal arguments are created at the Actual and formal arguments are created
different memory location at the same memory location
Concatenation of Strings
• Concatenation = Gluing together
• S1=“Hello, World”
• S1=S1 * 2
Substring = String slicing
replace(‘A’) or replace(“B”)
Single Quote or Double Quote
All occurrences are replaced.
The split() method