Functions in programming.

What are functions:

Functions are a defined code for some repeated procedures or work. If we want to use a repeated code in our code or program, then we can make a defined function and give it a suitable name.

Whenever we want to use that code in our program, we can just call it by name. No need to write the same code again and again.

How to define a function in python ?

below is the syntax to define function in python

def hello():

      print("hello")

 

#start of program

Print("visitor")

hello()

We can define a function by using keyword def after that name of the function and supply any parameters in parenthesis if required. Then type : colon. After colon body of the function starts, here we write the code required for our work.

In our case we have written hello using print function in our function body.

Scope:

what is a scope in function. Scope means the range of code where a function can be called to do defined task. Some functions can be defined privately so that they are visible to required Programs or projects only.

Parameters in functions:

sometimes we need to pass some data for some required task or functionality in a function. We can pass that data with the help of variables we define and manipulate in our program. These variables are called Parameters in functions.

For example

Hello(name,age, country)

Here we are passing three variables in function calling. Hello is our function name.these variables must have been defined somewhere in the program before calling function. This is known as passing parameters to function definition.

Example below

Def greet(name):

     print('hello',name)

#start of program

name=input('enter your name')

greet(name)

In above example we have passed string variables name to greet function. We got data from input function and passed it to greet function. After that we called the function. The value of the name variables will be copied to name variable defined in function code.

Similarly we can have many parameters to function as required by task.

Scope of parameters of the function:

parameter variables used in the function are local to the function. It means they can only be used or accessed within function only. They can not be used outside function. See example below.

def numbershow(a):

    a=5

    print (a)

#start of program

a=10

numbershow(a)

print(a)

In above example last line will show 10 only, why?.

because we have passed the copy of variable a to function. We manipulated the value in local copy of variable in function, but that did not change the value of actual variable. It shows 5 within the function, beacuse it is functions local copy.

As soon as function finishes execution it's variable also deleted. 

Passing Arrays to functions:

for this section I consider you know arrays .arrays are list of similar data. In python we will use List in place of arrays. Below is an example of list

data=[10,2,5,6,'N']

See below how can pass List(arrays) to functions

def mylist(data):

    print(data)

#start of program

data=[10,2,5,6,'N']

mylist(data)

print('printed the data using function')

Scope of parameters in lists:

Any change done in list data in function will be changed in the actual variable that is passed to function in program.

Try yourself.

Return value in functions:

some times we want to manipulate variables in the functions and get that manipulated value back in calling program. Here we use return keyword to pass back value from function to calling program.

See example below:

def changevalue(a):

    a=20

    return(a)

#start of program

a=10

a=changevalue(a)

print(a)

In above example we get the changed value back in variables a in calling program.

Functions using libraries:

in python we have many libraries. Many functions for programming are already defined there. libraries are a set of similar functions defined within a name. We can access or call those functions by using import keyword in starting of program followed by name of library. See example below.

Import Math

b=Math.sqrt(25)

Print('square root is',b)

Similarly there are string manipulation Libraries and many more. You can explore themselves.

 

Thank you.