CBSETest.comby Bimal Publications

Need help with Functions?

Practice Tests
Class 11 · Computer Science NCERT Class 11 Computer Science · Ch. 74 min read · 15 questions

Functions

Computer Science

Functions

Functions in Python

A function is a named, reusable block of code that performs a specific task. Functions help us avoid writing the same code repeatedly and make programs easier to read, test, and maintain. Python supports two broad categories: built-in functions (like print(), len(), type()) and user-defined functions that we create ourselves.

---

Defining and Calling a Function

Use the def keyword to define a function, followed by the function name, parentheses, and a colon. The indented block below is the function body.

```
def greet():
print("Hello, World!")

greet() # calling the function
```

---

Parameters and Arguments

A parameter is a variable listed in the function definition. An argument is the actual value passed when the function is called.

Example 1

Function with one parameter
```
def greet(name):
print("Hello,", name)

greet("Priya") # Output: Hello, Priya
```

Example 2

Function with multiple parameters
```
def add(a, b):
return a + b

result = add(3, 5)
print(result) # Output: 8
```

---

The return Statement

The return statement sends a value back to the caller. A function without return (or with bare return) gives back None.

Example 3

Returning a computed value
```
def square(n):
return n · n

print(square(6)) # Output: 36
```

---

Default Parameters

You can assign a default value to a parameter. If the caller does not supply that argument, the default is used.

Example 4

Default parameter
```
def power(base, exp=2):
return base · · exp

print(power(3)) # Output: 9 (exp defaults to 2)
print(power(2, 10)) # Output: 1024
```

---

Keyword Arguments

Arguments can be passed by name (keyword arguments), allowing any order.

Example 5

Keyword arguments
```
def describe(name, age):
print(name, "is", age, "years old")

describe(age=17, name="Arjun") # Output: Arjun is 17 years old
```

---

Scope: Local vs Global Variables

A variable created inside a function is local — it exists only within that function. A variable created outside all functions is global.

Example 6

Scope demonstration
```
x = 10 # global

def show():
x = 99 # local — does NOT change global x
print(x) # 99

show()
print(x) # 10 — global unchanged
```

Use the global keyword inside a function to explicitly modify a global variable.

---

Docstrings

A docstring is a string literal placed as the first statement in a function body to document its purpose. Access it via functionName._doc__.

Example 7

Docstring usage
```
def multiply(a, b):
"""Return the product of a and b."""
return a · b

print(multiply._doc__)
```

---

Common mistakes

  • Forgetting to call the function — defining it does nothing on its own.
  • Returning vs printing — a function that only prints cannot be used in expressions.
  • Positional argument order — mixing up argument positions causes logic errors.
  • Mutable default arguments — using a list as a default parameter shares it across calls; use None and create inside the function instead.

---

Summary

Functions are defined with def, accept parameters, and use return to send values back. Default and keyword arguments add flexibility. Variables inside functions are local by default, and docstrings document what a function does.

Practice Problems

15 questions with instant feedback.

Question 1 of 15Score 0

Which keyword is used to define a function in Python?