functions

16
! " Function Function is self-contained block of program that perform specific task and return certain values. Syntax: <return type><function name> (argument list); - Return type is either void or int types Every “C” program start with main () function which is user defined functions. - C language supports two types of functions. - I) Library function - II) User defined functions Library Function: these are pre-defined functions in different header file. e.g: printf (),scanf(),gets(),sqrt(),pow() etc. e.g: #include<stdio.h> #include<math.h> Void main () { int no, r; printf (“Enter a number”); scanf (“%d”,&no); r=sqrt(no); printf(“%d”,r); } User defined function: They are defined by the user according user requirement. A user defined function has three things. i) Function prototype ii) Function call iii) Function definition

Upload: santosh-rath

Post on 20-Jan-2015

96 views

Category:

Education


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

������

Function Function is self-contained block of program that perform specific task and return certain values. Syntax: <return type><function name> (argument list);

- Return type is either void or int types

Every “C” program start with main () function which is user defined functions.

- C language supports two types of functions. - I) Library function - II) User defined functions

Library Function: these are pre-defined functions in different header file. e.g: printf (),scanf(),gets(),sqrt(),pow() etc. e.g: #include<stdio.h> #include<math.h> Void main () { int no, r; printf (“Enter a number”); scanf (“%d”,&no); r=sqrt(no); printf(“%d”,r); } User defined function: They are defined by the user according user requirement. A user defined function has three things.

i) Function prototype ii) Function call iii) Function definition

Page 2: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

������

Why use function: if we want to perform a task repeatedly then it is not necessary to re-write the particular block of the program again and again. -Shift the particular block of statement in a user defined function. the function defined can be used for any number of times to perform the task.

- Use the function can be reduced to smaller one. - It is easy to debug and find out the error. - It also increases readability.

How it works: whenever a function is called control passes to the called function and working of the calling function is stopped.

- When the execution of function is completed, control returns to the calling function and execute the next statement.

- The value of actual argument passed by the calling function and received by the formal argument of the called function. The no of actual argument and formal argument are same.

- The function operates on formal argument and sends back the result to the calling functions, the return () statement performs this task.

Syntax to Declare the Calling Function: <return type> <function name> (argument list with its data type); Argumnent –declaration; { Statment1; Statment2; --------------- ---------------- Return(values); } Actual argument: the argument calling function are actual argument.

Page 3: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

������

Formal argument: the argument of called function is formal argument. Argument List: the argument list means various names enclosed with in parenthesis. They must separated by comma. Function Prototype:

- By default the function can pass integer type of data and return integer type.

- Other than type of data, the function cannot pass and return. In such cases we declare the function. Such definition of the function is known as function prototype.

- The prototype of these function are given in header file. Those we including using # include directives.

- In c while defining is defined function it must declare the prototype.

- A prototype declaration consist of function return type ,name, and arguments list.

- When programmers define the function of the function is same as prototype declaration.

- If the programmer makes do any mistake, the compiler shows the error message. The function prototype statement always ends with semicolon.

Syntax: Void main () { Variable declaration/function declaration; Value initialization; Calling function Printing result; } Function definition/return type function name (argument list)

Page 4: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

������

{ Formal argument declaration; Operation; Return result; } Function declaration is four types:

1) Function with return type with arguments. 2) Function without return type with arguments 3) Function with return type without arguments 4) Function without return type without arguments.

Example: W.A.P add two numbers. 1) Function with return type with arguments.

Void main() { Int add(int,int);

Int a,b,c; Calling function Printf(“Enter two numbers a & b”); Scanf(“%d%d”,&a,&b); C=add(a,b); } Int add(int x,int y) { Int z; Called function Z=x+y; Return(z); }

Note: In this example a & b are the actual arguments x, y are the formal arguments.

Page 5: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

������

2) Function without return type with arguments. Void main() { void add(int,int);

Int a,b; Calling function Printf(“Enter two numbers a & b”); Scanf(“%d%d”,&a,&b); add(a,b); } Int add(int x,int y) { Int z; Called function Z=x+y; Printf (“%d”,z); }

Note: In this example a & b are the actual arguments x, y are the formal arguments.

3) Function with return type without arguments. Void main() { int add( void ); calling function add(a,b); } Int add(void) { Int x,y,z; Printf(“Enter two numbers x & y”); Scanf(“%d%d”,&x,&y); Called function Z=x+y; Return (z); }

Note: Void means null/no arguments

Page 6: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

������

4) Function without return type without arguments. Void main() { void add( void ); calling function add(a,b); } void add(void) { Int x,y,z; Printf(“Enter two numbers x & y”); Scanf(“%d%d”,&x,&y); Called function Z=x+y; Printf (“%d”,z); }

Note: Void means null/no arguments Return Statements: It is jump control statement, when it executes then the control return to calling function. Syntax: Return; it means that it returns statement without values. Return (value); it means that return a statement with a value.

- Return statement assign value. - There is more than one return statement present in a function

body with conditions.

Page 7: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

������

Program: W.A.P input Two no’s and then swap them. #include<stdio.h> #include<conio.h> void main() { void swap(int,int); int a,b; clrscr(); printf("Enter two no"); scanf("%d%d",&a,&b); swap(a,b); //printf("%d%d",a,b); getch(); } void swap(int x,int y) { int z; z=x; x=y; y=z; printf("x=%d y=%d",x,y); } Program: W.A.P input a no and then reverses it. #include<stdio.h> #include<conio.h> void main() { int rev(int); int no,r; clrscr(); printf("Enter a no");

Page 8: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

������

scanf("%d",&no); r=rev(no); printf("%d",r); getch(); } int rev(int no1) { int r1=0,dg; while(no1>0) { dg=no1%10; r1=r1*10+dg; no1=no1/10; } return(r1); } Program: W.A.P input a no and then check palindrome or not. #include<stdio.h> #include<conio.h> void main() { int pali(int); int no,r; clrscr(); printf("Enter a no"); scanf("%d",&no); r=pali(no); if(r==no) printf("palindrome"); else printf("Not palindrome"); getch();

Page 9: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

��

} int pali(int no1) { int r1=0,dg; while(no1>0) { dg=no1%10; r1=r1*10+dg; no1=no1/10; } return(r1); } Program: W.A.P to find out X ^ Y. #include<stdio.h> #include<conio.h> #include<math.h> void main() { int power(int,int); int x,y,n; clrscr(); printf("Enter two no x & y"); scanf("%d%d",&x,&y); n=power(x,y); printf("%d",n); } int power(int a,int b) { int n1=1; while(b>0) { n1=n1*a;

Page 10: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

������

b--; } return(n1); } Program: W.A.P to find out factorial of a no. #include<stdio.h> #include<conio.h> void main() { int fact(int);//function prototype int no,f; clrscr(); printf("Enter a no"); scanf("%d",&no); f=fact(no);//actual argument printf("%d",f); } //Function Defination int fact(int no1)//formal argument { int f1=1; while(no1>0) { f1=f1*no1; no1--; } return(f1); }

Page 11: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

����������

Program: W.A.P to find out biggest no among three no’s #include<stdio.h> #include<conio.h> void main() { int biggest(int,int,int);//function prototype int a,b,c,d; clrscr(); printf("Enter three nos"); scanf("%d%d%d",&a,&b,&c); d=biggest(a,b,c);//actual argument printf("biggest=%d",d); } //Function Defination int biggest(int x,int y,int z)//formal argument { if(x>y&&x>z) return(x); else if(y>x&&y>z) return(y); else return(z); } Program: C code to print Fibonacci series without recursion: #include<stdio.h> void printFibonacci(int); int main(){ int k,n; long int i=0,j=1,f; printf("Enter the range of the Fibonacci series: "); scanf("%d",&n);

Page 12: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

����������

printf("Fibonacci Series: "); printf("%d ",0); printFibonacci(n); return 0; } void printFibonacci(int n){ long int first=0,second=1,sum; while(n>0){ sum = first + second; first = second; second = sum; printf("%ld ",sum); n--; } }

Recursion

1. A recursive function is a function which calls itself. 2. The speed of a recursive program is slower because of stack

overheads. (This attribute is evident if you run above C program.)

3. A recursive function must have recursive conditions, terminating conditions, and recursive expressions.

Program: Calculating factorial value using recursion To understand how recursion works lets have another popular example of recursion. In this example we will calculate the factorial of n numbers. The factorial of n numbers is expressed as a series of repetitive multiplication as shown below: Factorial of n = n (n-1)(n-2)……1. Example: Factorial of 5 = 5x4x3x2x1=120

Page 13: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

����������

#include<stdio.h> #include<conio.h> int factorial(int); void main() { int x; clrscr(); printf("Enter any number to calculate factorial :"); scanf("%d",&x); printf("\nFactorial : %d", factorial (x)); getch(); } int factorial (int i) { int f; if(i==1) return 1; else f = i* factorial (i-1); return f; } Explanation: Suppose value of i=5, since i is not equal to 1, the statement: f = i* factorial (i-1); Will be executed with i=5 i.e. f = 5* factorial (5-1); Will be evaluated. As you can see this statement again calls factorial function with value i-1 which will return value: 4*factorial (4-1); This recursive calling continues until value of i is equal to 1 and when i is equal to 1 it returns 1 and execution of this function stops. We can review the series of recursive call as follow: f = 5* factorial (5-1);

Page 14: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

����������

f = 5*4* factorial (4-1); f = 5*4*3* factorial (3-1); f = 5*4*3*2* factorial (2-1); f = 5*4*3*2*1; f = 120;

Program: C code to find the addition of n numbers by recursion #include<stdio.h> int main() { int n,sum; printf("Enter the value of n: "); scanf("%d",&n); sum = getSum(n); printf("Sum of n numbers: %d",sum); return 0; } int getSum(n) { int sum=0; if(n>0){ sum = sum + n; getSum(n-1); } return sum; } Sample output: Enter the value of n: 10 Sum of n numbers: 55

Page 15: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

����������

Program: Fibonacci series in c by using recursion

#include<stdio.h> void printFibonacci(int); int main(){ int k,n; long int i=0,j=1,f; printf("Enter the range of the Fibonacci series: "); scanf("%d",&n); printf("Fibonacci Series: "); printf("%d ",0); printFibonacci(n); return 0; } void printFibonacci(int n){ static long int first=0,second=1,sum; if(n>0){ sum = first + second; first = second; second = sum; printf("%ld ",sum); printFibonacci(n-1); } } Sample output: Enter the range of the Fibonacci series: 10 Fibonacci Series: 0 1 2 3 5 8 13 21 34 55 89

Page 16: Functions

�������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ����������������� ������� �������� � ��������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ��������������������������������������������������������������������������������������� ��� ��� � ���������������������������������������������������������������������������������

� ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ ��������� �������� ������� ���� � ������������������ � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ��������������������������������� � ���� � ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� ���� ���� ���� �� ����� ���� ����� ���� ����� ���� ����� ����!����������������������������������������!����������������������������������������!����������������������������������������!��������������������������������������������������������������������������������������������������������������������������������

� ���� ���� ���� ���""""���������

����������

Program: Reverse A Number Using Recursion In C Program

#include<stdio.h> int main(){ int num,rev; printf("\nEnter a number :"); scanf("%d",&num); rev=reverse(num); printf("\nAfter reverse the no is :%d",rev); return 0; } int sum=0,r; reverse(int num){ if(num){ r=num%10; sum=sum*10+r; reverse(num/10); } else return sum; return sum; }