Working with functions new Computer Science easy notes Class 12

Working with functions article is dedicated to class 12 computer science of CBSE schools. Functions are a very important part of python programming.

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

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 program
  • Reduce the repeated statements
  • Ambiguity can be reduced
  • Make program more readable and understandable

2 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

3 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 at interactive mode: 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 like this:
  • 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

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

5 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:

Practice Questions for working with functions

  1. Write a python function to accept your name and date of birth. Find your age on today’s date.
  2. Write a python function to convert money into Rupees and Paise separately. For example, if money entered is 100.50 then it should print 100 Rs. and 50 Paise.
  3. Write a python function to check whether a number is a Palindrome or not.
  4. Write a python function to find the factorial of the entered number.
  5. Write a python function to accept student data of an exam and print the complete result.

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 comment section or share your thoughts in comment section as well.

Comment Your Views

%d bloggers like this: