Function, Method and Array

Codynn
3 Min Read

Function:

A group of statements which perform a specific task is called function. A function breaks the program into smaller parts which makes the program more systematic and organized. 

Basic Syntax:

#function header(A function starts with a keyword def with a unique function name)
def function(name):
  print( "My name is " + name)

#Calling Function
function("Subid")

Function components:

i. Keyword def

ii. Unique function name

iii. Parameters or arguments

iv. Colon (:)

v. Optional documentation string (docstring)

vi. Body statement with same indentation (usually four spaces)

vii. Optional return statement

Calling a function?

Calling a function helps in code reusability. Once we define a function, we can call it from another function or program. While calling a function, the function name with the appropriate parameter should be mentioned.  

Let’s be clear by the example.

def sum(a, b):
    c = a + b
    print("The sum of " + str(a) + " and " + str(b) + " is " + str(c) + ".")

#calling a function
sum(10, 20)

In the above program the function is defined as sum with an argument a and b. The c variable is initialized as (a+b) for processing output. After that, the function is called with the parameter 10 and 20.

Program:

def add_num(a,b):#function with parameter a and b 
    sum=a+b;
    return sum; #return value
number1= float(input("Enter first number: "))  #variable declaration
number2= float(input("Enter second number: "))
print("The sum is",add_num(number1,number2))#call the function

Output:

Enter first number: 67.5
Enter second number: 2.5
The sum is 70.0

Method:

The object consists of behavior and properties in object oriented programming. The method is used for defining the behavior of an object. Methods are reusable and can be used in any point of the program. There are three types of methods in python. They are instance methods, class methods, and static methods. 

Basic Syntax:

class Car:
          def __init__(self,brand,model,color,fuel):
                  self.brand=brand
                  self.model=model
                  self.color=color
                  self.fuel=fuel
           def start(self):
                  pass
           def halt(self):
                  pass
           def drift(self):
                  pass
           def speedup(self):
                  pass
           def turn(self):
                  pass

Array:

Arrays are not supported in Python by default, but list can be used instead of array. It is used for storing multiple values in a single variable. Eg: letters= [“A”, “B”, “C”]. Many values can be stored in an array with a single name, and the values can be accessed by referring to an index number.

Accessing Elements of an Array:

Program:

letters= ["A", "B", "C"] 
x = letters[0] 
print(x)

Output:

A

In the above program, elements of an array of letters are accessed. Array index 0 is accessed where we get output as A because array index starts from 0.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *