Zed-King Institute
    All Notes

    O Level · Chapterwise Notes

    Chapter 6: Functions

    Zed-King Study app · Free

    O Level students across India practise here, free

    Daily M1–M4 quizFull 100-question mocksAll-India rank
    Open the free app

    Google login · no Play Store needed

    🏆 Free All-India Sunday Mock · Sun, 29 Nov, 11 AM · CCC + O LevelRegister

    About this PDF

    User-defined functions, arguments, recursion and built-in functions.

    This is part of the free O Level M3-R5.1 study material in the Zed-King Study Zone. You can read every page above in your browser, or download the file and study offline. Zed-King Group of Institute is a NIELIT Study Centre in Kaithal, Haryana, and all of this material follows the current NIELIT syllabus.

    Course
    O Level — NIELIT Foundation Diploma
    Module
    M3 — M3-R5.1: Programming and Problem Solving through Python
    Section
    Chapterwise Notes
    Format
    PDF — read online or download
    Price
    Free, no sign-up required

    Text from this PDF

    The opening pages as plain text — open the reader above for tables, diagrams and the full file.

    CHAPTER-6 Functions Function:- A function is a program that performs a specific task. Python function is a group of statements. Every program has a function. Wherever the statements of the function are needed, the function is called.

    "Function is a block of code which only runs when it is called".

    Advantages of function:-

    • The code written in the function does not have to be written again and again

    • Large programs can be divided into small functions

    • Function saves the programmer's time, program's space and memory

    • If an error occurs in the program, it can be easily removed

    • The function can be called again and again where needed Types of Function in Python:-

    1. In-Built/Predefined Function

    2. User-Defined Function In-Built/Predefined Function It is also called library function. The function which is already defined in the python library is called library function. There are many types of library functions in User-Defined Function These are the functions which are created by the user. Users create these functions according to their requirements. The function which is defined by the user is

    called user defined function.

    def function_name(parameters):

    # function body # What the function does goes here # write some action return result Hint:- def:- Defining a function in Python begins with the def keyword Function_name:- We can give any name in it and later we can call the function with the same name.

    Parameters:- After the function name, you can pass O or more parameters between parentheses () which are given as values, arguments at the time of function calling Note:- A colon (:) must be placed after the parentheses ( )

    Function body:- The function body is an indented block of code that contains statements that perform a specific task Return result:- The result is returned through this

    1. wap to print "Hello world" & your name using function.

    def msg():

    def name():

    name

    2. wap to find addition of two number using functions.

    def add():

    a=int(input("Enter first number 'J))

    b=int(input("Enter second number 'l))

    print("Addition is= ",c)

    add()

    3. wap to check entered number is even nurnber or Odd nun•ber using function def ch():

    n=int(input("Enter any number "))

    print("Even No")

    ch()

    4. wap to find factorial Of any number using function def fact():

    n=int(input("Enter any number "))

    fact = 1 while n >O:

    fact = fact *n n=n—l print("Factorial is : ",fact)

    fact()

    return statement:- The return statement is used to return the value to the calling function. Upon exit from the called function, the execution control is transferred to the calling function. Return is a keyword which can be used anywhere in the body of the function.

    def add():

    a=int(input("Enter first number b=int(input("Enter second number return c res=add()

    print("Addition is = ",res)

    1. wap to find factorial of given number using return statement.

    Argument:- The value or variable given inside the bracket ( ) of the function is called argument. It is of two types

    1. Actual argument

    2. Formal argument Actual Argument:- The argument which is written within the bracket ( ) of the calling function is called the actual argument Formal Argument:- The argument which is written within the bracket ( ) of the called function is called the formal argument def add(a,b):

    print(c)

    > formal argument > actual argument Note: 10, a store OTT, 20, Types of argument in python:- There are four types of arguments in Python

    1. Positional or required arguments

    2. Default argument

    3. Keyword argument

    4. Arbitrary or variable-length arguments Positional Argument:- This type of argument function is provided in current positional order. If the position of argument in the function argument is changed then the result also changes.

    def add(a,b):

    d=a-b print(c)

    print(d)

    Default Argument:- By default value can also be provided in positional argument. If no value is given to the function then only default value is passed.

    def c:a+b; print(c)

    add(20)

    Keyword Argument:- The argument of the function can also be passed by using the keyword value def info( name, age ):

    # by keyword arguments info(name="anil", age = 30)

    info(age=25,name='akhilesh')

    Note:- i. The value will not change if the position changes.

    More chapterwise notes for M3-R5.1

    Run these Python programs yourself — free

    Reading code is not the same as writing it. Type any program from these notes into our free online Python compiler (real Python 3 with NumPy, input() works), or study one of the explained programs with its output.

    Prepare for O Level with expert guidance

    Classroom training, practical labs and exam registration at Zed-King Institute, Kaithal.

    Drag me!