• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 3 out of 17 pages
Class notes

Function and string in python

Document preview thumbnail
Preview 3 out of 17 pages

you can revise from this note for class 1 1 exam

Content preview

Class XI Computer Science (083)

Functions
❖ FUNCTIONS

In programming, Function can be defined as a named group of instructions
that accomplish a specific task when it is invoked. Once defined, a
function can be called repeatedly from different places of the program
without writing all the codes of that function every time, or it can be
called from inside another function, by simply writing the name of the
function and passing the required parameters. The programmer can define
as many functions as desired while writing the code.

❖ The Advantages of Function

• Increases readability, particularly for longer code as by using
functions, the program is better organized and easy to understand.
• Reduces code length as same code is not required to be written at
multiple places in a program. This also makes debugging easier.
• Increases reusability, as function can be called from another function
or another program. Thus, we can reuse or build upon already defined
functions and avoid repetitions of writing the same piece of code.
• Work can be easily divided among team members and completed in parallel.

❖ Types of Functions


Function




Standard Library User Defined




Built-in Module



❖ USER DEFINED FUNCTIONS

Taking advantage of reusability feature of functions, there is a large
number of functions already available in Python under standard library.
We can directly call these functions in our program without defining them.
However, in addition to the standard library functions, we can define our
Pre. by Pijush Page 1 of 17

,Class XI Computer Science (083)
own functions while writing the program. Such functions are called user
defined functions. Thus, a function defined to achieve some tasks as per
the programmer's requirement is called a user defined function.

❖ Creating User Defined Function

A function definition begins with def (short for define). The syntax for
creating a user defined function is as follows:

def<Function name> ([parameter 1, parameter 2, ….]) : Function Header
set of instructions to be executed
[return <value>] : Function Body

• The items enclosed in "[ ]" are called parameters and they are optional.
Hence, a function may or may not have parameters. Also, a function may
or may not return a value.
• Function header always ends with a colon (:).
• Function name should be unique. Rules for naming identifiers also
applies for function naming.
• The statements outside the function indentation are not considered as
part of the function.

#Function to add two numbers
#The requirements are listed below:
#1. We need to accept 2 numbers from the user.
#2. Calculate their sum
#3. Display the sum.

#function definition
def addnum():
fnum = int(input("Enter first number: "))
snum = int(input("Enter second number: "))
sum = fnum + snum
print("The sum of ",fnum,"and ",snum,"is ",sum)

#function call
addnum()

In order to execute the function addnum(), we need to call it. The function
can be called in the program by writing function name.

❖ Arguments and Parameters

In the above example, the numbers were accepted from the user within the
function itself, but it is also possible for a user defined function to
receive values at the time of being called. An argument is a value passed
Pre. by Pijush Page 2 of 17

, Class XI Computer Science (083)
to the function during the function call which is received in corresponding
parameter defined in function header.

#Program to find the sum of first n natural numbers
#The requirements are:
#1. n be passed as an argument
#2. Calculate sum of first n natural numbers
#3. Display the sum

#function header
def sumSquares(n): #n is the parameter
sum = 0
for i in range(1,n+1):
sum = sum + i
print("The sum of first",n,"natural numbers is: ",sum)

num = int(input("Enter the value for n: "))
#num is an argument referring to the value input by the user
sumSquares(num) #function call


(A) String as Parameters

In the above programs, the arguments passed are of numeric type only.
However, in some programs, user may need to pass string values as an
argument.
For example, if first name is Gyan and lastname is Vardhan, the output
should be:
Hello Gyan Vardhan
#Function to display full name
#The requirements are listed below:
#1. The function should have 2 parameters to accept first name and
#last name.
#2. Concatenate names using + operator with a space between first
#name and last name.
#3. Display full name.

def fullname(first,last):
#+ operator is used to concatenate strings
fullname = first + " " + last
print("Hello",fullname)
#function ends here
first = input("Enter first name: ")
last = input("Enter last name: ")

#function call
fullname(first,last)
Pre. by Pijush Page 3 of 17

Document information

School year
3
Uploaded on
November 5, 2026
Number of pages
17
Written in
2026/2027
Type
Class notes
Professor(s)
Pijush
Contains
All classes
$22.49

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
3
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions