Working with functions article is dedicated to class 12 computer science of CBSE schools students. Functions are a very important part of python programming.
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 few statements and instructions written in its body.
For example, if A school is organizing 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 in does for the python program.
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 program
- Reduce the repeated statements
- Ambiguity can be reduced
- Make 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
|Function Header||Always starts with the “def” keyword followed by the function name and its parameters, ends with a colon (:)|
|Parameters||Variables itemized 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|
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 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 Function call
- A function in the python program is called by 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 control the transfer
- Within the execution frame, the statements written in the function body executed and return a value or execute the last statement
- Python follows top to bottom approach for executing program
- Comments are ignored in execution
- If python notices 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
Watch this video for more details:
Working with functions in python class 12 programs
- Write a python function to accept your name and date of birth. Find your age on today’s date.
- Write a python function to convert money into Rupees and Paise separately. For example, if the money entered is 100.50 then it should print 100 Rs. and 50 Paise.
- Write a python function to check whether a number is a Palindrome or not.
- Write a python function to find the factorial of the entered number.
- Write a python function to accept student data of an exam and print the complete result.
Follow this link for questions:
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 set of instructions to be executed to fulfill the user’s tasks.
What are the types of functions in Python?
There are two types of function sin 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 def keyword followed by function name and parameters separated by commas.
Follow this syntax to write a function in Python 3:
What are Python built in functions?
Python built in functions are those functions which are stores in the python path library directory.