0% found this document useful (0 votes)
5 views

Function Part Two New

The document discusses the concept of passing arrays to functions in programming, detailing the differences between automatic (local) and external (global) variables. It also explains recursive functions, providing examples for calculating factorials and Fibonacci series. Key points include the importance of base conditions in recursion and the structure of recursive programs.

Uploaded by

ankitm1014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Function Part Two New

The document discusses the concept of passing arrays to functions in programming, detailing the differences between automatic (local) and external (global) variables. It also explains recursive functions, providing examples for calculating factorials and Fibonacci series. Key points include the importance of base conditions in recursion and the structure of recursive programs.

Uploaded by

ankitm1014
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Passing Arrays to Function

Learning Management System By Prakash Kshetri


Like the values of simple variables, it is also possible to pass the values of an array to function
…….
int largest(int a[], int n); // Function Prototype
int main()
{
…….
largest(a, n); //Function Call
return 0;
}
int largest(int a[], n) // Function Definition
{
……….
………..
}

Learning Management System By Prakash Kshetri


Learning Management System By Prakash Kshetri
Concept of Storage
The Scope, Visibility and Life Time of Variables
(Automatic, External, Static, Register)

Learning Management System By Prakash Kshetri


Automatic Variables (Local Variables)

• Automatic variables are declared inside a function.


• They are created when the function is called and destroyed automatically when the function is
exited, hence the name automatic.
• These variables are local or private to the function in which they are declared.
• Because of this property, automatic variables are also referred to as local or internal variables.
main ( )
{
int number;
// auto int number;
………
}

Learning Management System By Prakash Kshetri


Learning Management System By Prakash Kshetri
External Variables (Global Variables)
• Variables that are both alive and active throughout the entire program are known as external or
global variables.
• Unlike local variables, global variables can be accessed by any function in the program. External
variables are declared outside a function.

int number=500; // External or Global Variable


int main()
{
…….
}
int function1(void)
{
……
}

Learning Management System By Prakash Kshetri


Learning Management System By Prakash Kshetri
Difference between Automatic Storage Class and External Storage Class

Automatic Storage Class External Storage Class

➢ Variables are declared within function. ➢ Variables are declared outside function.

➢ The scope of variable is within the function. ➢ The scope of variable is throughout the program

➢ The initial value of the variable is ➢ The initial value is zero.


garbage/unpredictable.

➢ The life-time of the variable is the control that ➢ The life-time of the variable is the program’s execution
remains within the function in which the variable is comes to the end.
defined.

➢ The optional keyword auto can be used to declare. ➢ The optional keyword extern can be used to declare.

Learning Management System By Prakash Kshetri


Recursive Function
(Factorial and Fibonacci Problems)

Learning Management System By Prakash Kshetri


What is recursive function?
• Recursion is a process by which a function calls itself repeatedly. A
very simple example of recursion is presented below:
Output of Program, creates infinite loop

Learning Management System By Prakash Kshetri


• So, recursion is a process by which a function calls itself repeatedly,
until some specific condition has been satisfied.
• For the problem to solve recursively two conditions must be satisfied:
a. The problem must be written in a recursive form.
b. The problem statement must include a stopping condition
(Specify the base condition to stop the recursion).

With the help of these two conditions, we can write the recursive
program. Let’s implement these two steps in program.

Learning Management System By Prakash Kshetri


• Problem: To calculate the factorial of a number.
• Such as factorial of 5 (5!) = 5 x 4 x 3 x 2 x 1 = 120
• Let’s see the basic structure of recursive program:
factorial( )
{
if(condition)
{
Base Case >> 2
…..
}
else
{
Recursive Procedure >> 1
….
}
}
Learning Management System By Prakash Kshetri
Divide the problem into smaller sub-problems.
• To calculate factorial of 4 or factorial(4),
• factorial(1) = 1
• factorial(2)=2*1 = 2*factorial(1)
• factorial(3)=3*2*1 = 3*factorial(2)
• Factorial(4)=4*3*2*1=4*factorial(3)
Now, to calculate factorial(n),
• factorial(n) = n*factorial(n-1)

Learning Management System By Prakash Kshetri


int factorial(int n); //Function Prototype
Suppose user enters the value of n = 4,
int main()
{ ➢factorial(4)
int n, result; ➢return 4*factorial(3)
……. ➢Factorial(3)
scanf(“%d”,&n);
➢3*factorial(2)
result=factorial(n); //Function Call
printf(“Factorial = %d”,result); ➢factorial(2)
….. ➢2*factorial(1)
} ➢factorial(1)
int factorial(int n) //Function Definition ➢1
{
if(n<=1)
return 1;
else
return n*factorial(n-1);
}
Learning Management System By Prakash Kshetri
Learning Management System By Prakash Kshetri
Fibonacci Problems/Series
Using Recursive Function

Learning Management System By Prakash Kshetri


Fibonacci Series in C
• Fibonacci Series in C:
• In case of Fibonacci series, next number is the sum of previous two numbers
for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two terms of Fibonacci
series are 0 and 1.

• There are two ways to write the Fibonacci series program:


• Fibonacci Series without recursion
• Fibonacci Series using recursion

Learning Management System By Prakash Kshetri


Some Fibonacci Problems
• To calculate or find the nth term of Fibonacci
series using function.
• To display Fibonacci series up to nth term using
function.
• To calculate or find the nth term of Fibonacci
series using recursive function.
• To display Fibonacci series up to nth term using
recursive function.

Learning Management System By Prakash Kshetri


To calculate or find the nth term of Fibonacci series using recursive function.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ………. nth term.
int fibo(int num); //Function prototype
int main()
{ fibo(5)
int num,result;
………
scanf(“%d”,&num);
result = fibo(num); fibo(3) fibo(4)
……
return 0;
}
int fibo(int num) //Function Definition
fibo(1) fibo(2) fibo(2) fibo(3)
{
if(num == 1) //Base condition
return 0; fibo(1) fibo(2)
else if(num == 2) //Base condition
return 1;
else
return fibo(num-2) + fibo(num-1);
} Learning Management System By Prakash Kshetri
➢ To find 8th term of Fibonacci series:

fibo(8)

fibo(6) fibo(7)

fibo(4) fibo(5) fibo(5) fibo(6)

fibo(2) fibo(3) fibo(3) fibo(4)

fibo(1) fibo(2) fibo(1) fibo(2)


Learning Management System By Prakash Kshetri
To display Fibonacci series up to nth term.

Learning Management System By Prakash Kshetri

You might also like