CBSE Class 11 & 12 Computer Science and Informatics Practices Python Materials, Video Lecture

Introduction to Functions in Python (Class 12 Computer Science)

Introduction to Functions in Python (Class 12 Computer Science)


If you are looking for Python Functions Introduction and want to learn Python Functions from Basics, you have landed on the correct page. 

Here you will get to know the basics of Python Function, I will discuss all the basic details from What is Function? How to Write your Own Function

If you do not have any other computer programming knowledge, then Function terms will be new to you. You can consider function as something that does your work when you call it.

In Computer Programming you can Consider a Function as a block of code, it will do some Operation (Task) for you.

In CBSE Class 12 Computer Science syllabus, you will have to learn Python functions, Functions Parameters, and it's working. So, let's now discuss Functions in Python with technical details.

Introduction to Functions in Python

  • A function can be defined as a single related task, which is written as a block of codes.
  • It only runs when it is called.
  • We can pass data to the function when we call a function, these are known as parameters.
  • A function can return data as a result of using the return statement.
  • We have already used some python built-in functions like print(), len() etc. We can also create our own functions. These functions are called user-defined functions.
If you have not understood the above points don't worry. I will explain each point in a very simple and interesting way.

Let's consider a cook in your house as a Function. You provide necessary items (e.g. Vegetables, Oils, Flour, etc.) for cooking to your cook and he returns you the cooked (ready to eat) food.

In the same way a Function in programming a coding block where you provide necessary items (parameters) to your functions and it returns you the result. Sometime Parameters are also not required, it is as per the working of the functions.

Similarly, we create a function for a single related task. You can make the similarity with the cook as he will only make food (single related task) and may not do another task of yours (e.g. your office work).

Now we have talked enough about the function, we can now go into the coding part. We will write out First Python Function.

Writing and Calling the first Function

Writing and Calling the first Function

Above image shows the simplest Python function Example. In python, we used def keyword to define a function. After function name (DoSomething) we enclose it with small brackets, we can provide parameters inside these brackets. In this case, we have not provided any parameters.
We will use indented (with space) to write code inside a function, this is called function body. In the end, we have used the return keyword to return the result of the function's computation.

Writing and Calling the first Function

  • To call the function we will just write the name of the function with brackets (see the line -> var = dosomething() ).  When the python interpreter sees this line it calls the functions defined with this name.
  • dosomething() will we used to call the function (see the line -> def dosomething()) all the lines of the function will be executed line by line.
  • In the end, the result of our function will be returned (see the line -> return value). 
  • Now this result will be saved to var (see the line -> var = dosometing()).
  • In the above case, the return is returning the value variable whose value is 1. So, var will have 1 and the output will be printed as 1.
For more detail watch the below video:

For Practical knowledge see the below video:

No comments:

Post a Comment