Computer Science · Class 11 Optional
Ch 7Functions — Class 11 Computer Science, concept-first.
As programs grow to solve bigger problems, the number of lines of code grows with them. A long, single-block program quickly becomes bulky, hard to read and hard to maintain — spotting an error or changing one calculation means wading through the whole file.
Key concepts
Hover a concept to preview it and jump to its most relevant Q&A.
Simple Interest Calculation
Imagine you lend a friend ₹100. A year later, they return the ₹100. You're even — but you also missed the chance to use that ₹100 yourself for a whole year. That inconvenience has a price.
Most relevant Q&A
- Program 7-1: Program to calculate the payable amount for the tent.Preview
- Program 7-2: Program to calculate the payable amount for the tent using user defined functions.Preview
- Write a program that implements a user defined function that accepts Principal Amount, Rate, Time, Number of Times the interest is compounde…Preview
Chapter contents
The NCERT structure, section by section. Open a section to see its questions, then read the concept-first solution.
Introduction
As programs grow to solve bigger problems, the number of lines of code grows with them. A long, single-block program quickly becomes bulky, hard to read and hard to maintain — spotting an error or cha…
Functions
In programming, the function is one of the chief means of achieving modularity and reusability. A function can be defined as a named group of instructions that accomplishes a specific task when it is…
The Advantages of Function
Functions pay off the moment requirements change or grow — existing code can be reused instead of rewritten.
User Defined Functions
Because functions are reusable, Python ships with a very large number of them ready-made: the standard library (explored in Section 7.5) contains functions we can call in our programs without ever def…
Creating User Defined Function
A function definition always begins with the keyword def (short for define). The general syntax for creating a user defined function is:
Arguments and Parameters
6 QIn the previous example, addnum() collected its numbers from the user inside the function. A user defined function can instead receive values at the moment it is called.
+−Programs1 question
+−Programs1 question
+−Programs1 question
+−Programs1 question
+−Programs1 question
Functions Returning Value
A function may or may not send a value back when it is called. The return statement is what sends values out of a function.
Flow of Execution
3 QFlow of execution is the order in which the statements of a program are actually executed.
+−Programs1 question
+−Programs1 question
Scope of a Variable
2 QA variable defined inside a function cannot be used outside it. Every variable in a program has a well-defined region where it is accessible, and the part of the program in which a variable can be acc…
Python Standard Library
Python comes with a very extensive standard library — a large collection of ready-made functionality that can be called in a program as and when required.
Built-in functions
Built-in functions are the ready-made functions of Python — functions so frequently needed that they are permanently available in every program, with no import and no definition required.
Module
3 QBesides its built-in functions, the Python standard library contains a large number of modules. The distinction is one of level: a function is a grouping of instructions, while a module is a grouping…
+−Worked Examples1 question
+−Worked Examples1 question
Summary
- Functions are used in programming to achieve modularity and reusability. - A function is a named group of instructions that executes when the function is invoked (called) by its name.
Exercises
+−Show 7 questionsHide questions7 questions
- Q1Observe the following programs carefully, and identify the error: a) def create (text, freq): for i in range (1, freq): print text create(5)…Free
- Q2How is math.ceil(89.7) different from math.floor(89.7)?Free
- Q3Out of random() and randint(), which function should we use to generate random numbers between 1 and 5. Justify.Free
- Q4How is built-in function pow() function different from function math.pow() ? Explain with an example.Preview
- Q5Using an example show how a function in Python can return multiple values.Preview
- Q6Differentiate between following with the help of an example: a) Argument and Parameter b) Global and Local variablePreview
- Q7Does a function always return a value? Explain with an example.Preview
More questions
15 Q+−Show 5 questionsHide questions5 questions
- Q1To secure your account, whether it be an email, online bank account or any other account, it is important that we use authentication. Use yo…Free
- Q2XYZ store plans to give festival discount to its customers. The store management has decided to give discount on the following criteria: | S…Free
- Q3‘Play and learn’ strategy helps toddlers understand concepts in a fun way. Being a senior student you have taken responsibility to develop a…Preview
- Q4Take a look at the series below: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55… To form the pattern, start by writing 1 and 1. Add them together to get 2…Preview
- Q5Create a menu driven program using user defined functions to implement a calculator that performs the following: a) Basic arithmetic operati…Preview
+−Show 8 questionsHide questions8 questions
- Q1Write a program to check the divisibility of a number by 7 that is passed as a parameter to the user defined function.Free
- Q2Write a program that uses a user defined function that accepts name and gender (as M for Male, F for Female) and prefixes Mr/Ms on the basis…Free
- Q3Write a program that has a user defined function to accept the coefficients of a quadratic equation in variables and calculates its determin…Free
- Q4ABC School has allotted unique token IDs from (1 to 600) to all the parents for facilitating a lucky draw on the day of their Annual day fun…Preview
- Q5Write a program that implements a user defined function that accepts Principal Amount, Rate, Time, Number of Times the interest is compounde…Preview
- Q6Write a program that has a user defined function to accept 2 numbers as parameters, if number 1 is less than number 2 then numbers are swapp…Preview
- Q7Write a program that contains user defined functions to calculate area, perimeter or surface area whichever is applicable for various shapes…Preview
- Q8Write a program that creates a GK quiz consisting of any five questions of your choice. The questions should be displayed randomly. Create a…Preview