The working with Functions article is dedicated to class 12 computer science of CBSE schools students. Functions are a very important part of Python programming.
Topics Covered
Introduction to working with functions
Large programs need to be divided into smaller units. A function is a small unit of a program. It consists of different functions. A function has a few statements and instructions written in its body.
For example, if A school is organizing an Annual Day function which is a combination of some cultural programs and events. Here we can consider all the cultural programs and events as a function.
These functions will be executed on the Annual day in a specific order. In a similar way, it does for the Python program.
Function Definition
A function is a small unit of a program that processes the data and often returns a value.
The need for functions (working with functions)
- Easy program handling
- Reduce the size of the program
- Reduce the repeated statements
- Ambiguity can be reduced
- Make the program more readable and understandable
How to create a function in Python?
To create a function in Python consider the following parts of a function:
Parts of Function
Part | Description |
---|---|
Function Header | Always starts with the “def” keyword followed by the function name and its parameters, ends with a colon (:) |
Parameters | Variablessupplied in brackets of the function header |
Function Body | Block of statements/instructions that define the action performed by the function, indentation must be followed |
Indentation | White space at the beginning of every statement with the same block |
Function Calling | writing function name including parameter values |
The following five basic steps are used to create and invoke a function.
Watch the following for more details:
After writing the function it must be invoked through calling by following these steps:
- Save a program and click run or press the F5 button
- Now interactive mode will appear with the message RESTART ……
- Write a function call statement as shown in the below image
- A function call statement is just like a function name with required parameters
- Press enter and supply input as per requirements
Structure of Python Program
A Python program is a set of a few statements and blocks. A Python program may have the following:
- Physical line structure: A Python program is divided into no. of logical lines, the logical line is created from one or more physical lines
- Joining two lines: A logical line can be broken into two or more physical lines using a backslash ()
- Multiple statements on a single line: Semicolon (;) is used to write multiple statements on a single line
- The top-level statement or _main_: Unindented statements
- Comments: Begins with a hash symbol (#), python interpreter ignores them, multi-line comments will be written in “”” (triple-double quotes).
- Indentation: White spaces used at the beginning of every line. The indented part is known as one block.
The flow of Execution in the Function call
- A function in the python program is called by a function call statement
- To call a function, write the function name followed by parameter values in brackets
- A block of statements executed in the execution frame
- When a function is called, an execution frame is created and controls the transfer
- Within the execution frame, the statements written in the function body are executed and return a value or execute the last statement
- Python follows top to bottom approach for executing program
- Comments are ignored in the execution
- If Python notices a function definition with a def statement it just executes the function header line and skips all statements in the function body these statements execute when a function will be called
Let’s start with an understanding of the Python flow of execution in the function calls.
Watch this video for more details:
Follow this link for important questions on the explained topics:
Follow this link for the next lecture on function. The topics covered in these notes are:
- Python Function Types
- User Defined Functions
- Parameter and Arguments
- Rules for combining three types of arguments
- Returning Values
- Scope of Variable
- Mutable Immutable of Arguments and Parameter & Function Calls
Working with functions in Python class 12 solutions
Follow this link for questions:
Read python documentation from python reference manual. Return back to Computer Science contents.
Thank you for reading this post. Feel free to ask your doubts in the comment section or share your thoughts in the comment section as well.
FAQs on Working with functions
-
What are the functions in Python Class 12?
Functions in python Class 12 are the most important part of python programs. They are a set of instructions to be executed to fulfil the user’s tasks.
-
What are the types of functions in Python?
There are two types of functions in Python:
1. Built-in functions – Functions available in the inbuilt library of Python site-packages
2. User Defined functions – Functions Created by users -
How do you write a function in Python 3?
To write a function in Python 3, use the def keyword followed by function name and parameters separated by commas.
Follow this syntax to write a function in Python 3:
def functionname(parameters):
statementsExample:
def sum(a,b):
return a+b -
What are Python built in functions?
Python built in functions are those functions which are stores in the Python path library directory.