Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views3 pages

Assignment 4

A function in Python is a reusable code block that performs a specific task, defined with a name and parameters. Function calling is the execution of a function by its name, while the function called refers to the defined function being invoked. An example program demonstrates calculating the factorial of a number using recursion, where the function calls itself until reaching the base case.

Uploaded by

Sireesha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Assignment 4

A function in Python is a reusable code block that performs a specific task, defined with a name and parameters. Function calling is the execution of a function by its name, while the function called refers to the defined function being invoked. An example program demonstrates calculating the factorial of a number using recursion, where the function calls itself until reaching the base case.

Uploaded by

Sireesha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4.What is a function?

Explain function calling and


function called with suitable example? Write a
python program to find the Factorial Value using
function recursion?
ANS:
A function in Python is a block of reusable code designed to perform a
specific task. Functions allow you to divide complex problems into smaller,
manageable parts and help in reusing code throughout a program.

Key Concepts of Functions:


1. Function Definition: This is where the function is defined with a
specific name and any required parameters.
SYNTAX:
def function_name(parameters):
# Code block
return result
2.Function Call: This is where the function is executed (or called) by specifying
the function name followed by parentheses, optionally passing arguments to it.

 Function Calling: The point in the code where you call a function by its
name to execute the function’s code.
 Function Called: The function itself that is defined elsewhere and is
being invoked in the program.

Example of Function Definition and Calling

# Function definition
def greet(name):
print("Hello, " + name + "!")
# Function calling
greet("Alice")

Explanation:
 Function Definition (greet): Here, we define a function greet that takes a
parameter name.
 Function Call (greet("Alice")): We call greet with the argument "Alice",
resulting in "Hello, Alice!" being printed.

Factorial Program Using Recursion


Recursion is a technique where a function calls itself to solve a problem by
breaking it into smaller, similar subproblems. Calculating the factorial of a
number is a classic example of recursion.
Factorial of a positive integer nnn is the product of all positive integers from 1
to nnn. Mathematically, it’s defined as:
 n!=n×(n−1)×(n−2)×...×1n! = n \times (n-1) \times (n-2) \times ... \times
1n!=n×(n−1)×(n−2)×...×1
 Factorial of 0 is defined as 1.

Python Program to Find Factorial Using Recursion


# Function to find factorial using recursion
def factorial(n):
# Base case: factorial of 0 or 1 is 1
if n == 0 or n == 1:
return 1
else:
# Recursive case: n * factorial of (n-1)
return n * factorial(n - 1)

# Input from user


number = int(input("Enter a number: "))

# Function call
result = factorial(number)

# Display the result


print(f"The factorial of {number} is {result}")

Explanation:
1. Function Definition (factorial(n)): The factorial function takes a single
integer n.
o Base Case: If n is 0 or 1, it returns 1 (ending the recursion).
o Recursive Case: If n is greater than 1, it returns n * factorial(n - 1),
calling itself with n-1.
2. Function Call (factorial(number)): The function is called with the user's
input as number, and the recursive calls compute the factorial step-by-
step until the base case is reached.

SAMPLE OUT:
Enter a number: 5
The factorial of 5 is 120
In this example, if the user enters 5, the function will calculate
5×4×3×2×1=120

You might also like