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.

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 functions
Parts of functions

Parts of Function

PartDescription
Function HeaderAlways starts with the “def” keyword followed by the function name and its parameters, ends with a colon (:)
ParametersVariablessupplied in brackets of the function header
Function BodyBlock of statements/instructions that define the action performed by the function, indentation must be followed
IndentationWhite space at the beginning of every statement with the same block
Function Callingwriting function name including parameter values

The following five basic steps are used to create and invoke a function.

creating python function in just easy five steps

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
Function Calling
Function Calling

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.
Python Program Structure
Python Program Structure

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:

Questions working with a function computer science class 12

Follow this link for the next lecture on function. The topics covered in these notes are:

  1. Python Function Types
  2. User Defined Functions
  3. Parameter and Arguments
  4. Rules for combining three types of arguments
  5. Returning Values
  6. Scope of Variable
  7. Mutable Immutable of Arguments and Parameter & Function Calls

Types of functions

Working with functions in Python class 12 solutions

Follow this link for questions:

Click here to see the solved question for working with functions

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

  1. 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.

  2. 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

  3. 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):
    statements

    Example:
    def sum(a,b):
    return a+b

  4. What are Python built in functions?

    Python built in functions are those functions which are stores in the Python path library directory.

Leave a Reply