5 Easy Functions and scope Explanation in programming

Detailed view of programming code in a dark theme on a computer screen.

1. Purpose of functions in programming

Functions are just a block of code which we want to execute many times just by writing one line of code. The purpose of function in programming is to modularize code in small manageable blocks.

For Example:

def average(a, b, c):
    d = (a + b + c)/3
    return d

print(average(3, 5, 7)) # Output: 5
print(average(2, 3, 4)) # Output: 3

In above code you can see we have calculated of two list of numbers without applying logic twice with just one line of code which is function.

2. How to Pass arguments to a function

This is the important part of coding with functions is how to pass arguments in functions. arguments are just parameters of a functions like attributes of a html tag.

Arguments are the values on which function will execute further, like in the below code.

def sum(a, b): # a and b are parameters
    c = a + b # Using those parameters
    return c

print(sum(5, 1)) # Giving arguments (values)
print(sum(10, 30)) # Another Argument to the function

This is how you can pass arguments to a function. and also you learnt what is parameter and what is argument. parameters are which is defined by developer and arguments which user give to the function.

3. What Happens if a function does not return a value?

So there are four types of functions in python which are:

  1. Function without parameter
  2. Function with parameter
  3. Function with return type
  4. Function without return type

So don’t worry if you don’t want to use return in a function you can just use print inside function without using return type. Like code below

def sum(a, b):
    c = a + b
    print(c) # Using print instead of return type

# We can call function without using print
sum(4, 5) # output: 9

This is what happens when a function does not have return value. Or if a function is neither using print nor return then the function will not return any output like the code below

def sum(a, b):
    c = a + b

sum(4, 5) # Output: NULL

Here output will be empty because the function does not return a value.

4. Difference Between Local and Global Variable

The local variable which is initialized inside the function and cannot be called outside the function is called Local Variable. and Global Variable is the variable which is initialized outside the function and can be called anywhere in the program.

i = 10 # Global Variable
def fun():
    j = i + 10 # j is Local variable
    print(j) # Output: 20

z = j + 10 # It will give error because j is local variable which cannot be called outside the function.

This is the only difference between local and global variable in programming.

5. How can you call a function inside another function?

This is very easy you can call a Function inside a function as you call function outside the function. Like consider code below

def sum(a, b, c):
    d = a + b + c
    return d

def average(a, b, c):
    avg = sum(a, b, c)/3 # Calling sum function inside average function
    print(avg)

This is how easy calling function inside a function is. This is about programming Python.

functions in programming

So, If you reached at this level, please consider to leave a beautiful meaningful comment for appreciation and feedback, please share this post to your friends and classmates if you like this, Thank you for reading my blog. If you are interest in learning programming with me then you can visit my website and explore more about it or you can visit related posts given below right after this.

Leave a Comment

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

Scroll to Top