Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

C PROGRAMMING

FOR PROBLEM SOLVING


(18CPS23)
Module 4
User Defined Functions and Recursion

Prof. Mahalakshmi C V, Assistant Professor, CSE, BIT


Email: mahalakshmicv@gmail.com
Website: https://sites.google.com/site/mahalakshmicvbitcse
INTRODUCTION

A function is a block of code to perform a specific task.


• Every C program has at least one function main(). Without main() function,
there is technically no C program.
• In modular programming, the program is divided into separate small programs
called modules. Each module is designed to perform specific function.
Modules make our actual program shorter, hence easier to read and understand.
Advantages of functions:
• Reusability: Particular set of instructions can be used repeatedly from several
different places within the program.
• Easy debugging: since each function is smaller and has a logical clarity, it is
easy to locate and correct errors in it.
• Build library: Functions allows a programmer to build a customized library of
frequently used routines or system-dependent features. Each routine can be
programmed as a separate function and stored within a special library file.
Types of C Function

• There are 2 types of functions in C programming:


1. Library function
2. User defined function
Library function
Library functions are the in-build function in C compiler.
For example:
main() //the execution of every C program starts from this main() function
printf() //printf() is used for displaying output in C
sqrt(x); // finds the square root of x
scanf() //scanf() is used for taking input in C
User defined function
C allows programmer to define their own function according to their
requirement. These types of functions are known as user-defined functions.
Advantages
• User defined functions helps to decompose the large program into small
segments which makes programmer easy to understand, maintain and
debug.
• If repeated code occurs in a program. Function can be used to include
those code and execute when needed by calling that function.
• Programmer working on large project can divide the workload by making
different functions.
Function and Program Structure

#include<stdio.h>
global declarations;
void function_name(parameter list); //function declaration or function prototype
void main()
{
local declarations;
Statements;
Function_name(parameter list); //function call
...........
..........
}
void function_name(parameter list) //function definition
{
local declarations;
Statements;
}
• Where:
– return_type is the data type that the function returns.
– Function_name refers to the name of the function.
– The formal parameters are a comma separated list of variables that
receive the values from the main program when a function is called.
– The return statement returns the results of the function.
Program to add two integers. Marks a function add integers and display
sum in main() function.

#include<stdio.h>
int add(int a,int b); // function prototype
void main()
{
int a, b, sum;
printf(“enter two numbers\n”);
scanf(“%d%d”,&a,&b);
sum=add(a,b); //actual arguments
printf(“\n sum=%d”,sum);
}
int add(int a,int b) //formal arguments
{
int sum; //local declaration
sum=a+b;
return sum;
}
ARRAYS AND FUNCTION
2. Passing the whole Array
– Here , the parameter passing is similar to pass by reference.
– In pass by reference, the formal parameters are treated as aliases for actual
parameters.
– So, any changes in formal parameter imply there is a change in actual parameter.
• Example: program to find average of 6 marks using pass by reference.
include<stdio.h>
void average(int m[])
{
int i,avg;
int avg,sum=0;
for(i=0;i<6;i++)
sum=sum+m[i];
avg=(sum/6); Output:
printf(“aggregate marks=%d”,avg); aggregate marks=70
}
void main()
{
int m[6]={60,50,70,80,40,80,70};
average(m); //m is the base address
}
• Function with no arguments and with return value:
– In this , there is no data transfer from the calling function to the called function. But there
is a data transfer from called function to the calling function.
– When the function definition returns a value the calling function receives one value from
the called function.
Example: Program to illustrate function with no arguments and return value.
#include<stdio.h>
int add();
void main()
{
int sum;
sum=add ( );
printf(“\nsum=%d”,sum);
}
int add( )
{
int a,b,sum;
printf(“enter two numbers to add\n”);
scanf(“%d%d”,&a,&b);
sum=a+b;
return sum;
}
4. FUNCTION WITH ARGUMENTS AND RETURN VALUE:
– In this category there is a data transfer between the calling function and called function.
– When parameters are passed , the called function can receive values from the calling
function. When the function returns a value the calling function can receive a value from
the called function.
Example: program to illustrate function with arguments and return value
#include<stdio.h>
int add(int a, int b);
void main()
{
int a, b,sum;
printf(“enter two number to add\n”);
scanf(“%d%d”,&a,&b);
sum=add(a,b);
printf(“\n sum=%d”,sum);
}
int add(int a, int b)
{
int sum;
sum=a+b;
return sum;
}
Parameter Passing Techniques

• The calling and called function need to communicate with each other to
exchange the data.
• The data flow between the calling and called functions can be divided into
– Call-by-value
– Call-by-reference

• Call by value:
– It is a one-way communication. The calling function can send data to
the called function, but the called function cannot send any data to the
calling function.
– The called function may change the values passed, but the original
values in the calling function remain unchanged.
void swap(int a,int b);
• Call by reference:
– We may often come across situations where we need to modify the values of actual
parameter in the called function.

void swap(int *a,int *b);


C PROGRAM TO CHECK IF A NUMBER IS PRIME

#include<stdio.h>
#include<math.h>
void main()
{
int n,i,flag=1;
printf(“enter the number\”);
scanf(“%d”,&n);
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
flag=0;
break;
}
}
if(n<=1)
flag=0;
if(flag==1)
{
printf(“%d is a prime number”,n);
}
else
{
printf(“%d is not a prime number”,n);
}
}
Recursion

• A function that calls itself is known as recursive function.


• The rule to be followed for recursive function is
– Establish boundary conditions that terminate the recursive calls
– Implement the recursive calls so that each call brings one steps closer
to the solution.
• Every recursive function must be provided with a way to end the
recursion. In this example when , n is equal to 0, there is no recursive call
and recursion ends.
• Program to find factorial of a number using recursion.
#include<stdio.h>
int fact(int n)
Tracing
{ N=2
if(n==1) If(n==1) F
return 1; Return(2*fact(2-1))=return(2*1)=return(2)
N=1
return(n*fact(n-1)); If(n==1)
} Return 1
void main()
{
int n,res;
printf(“enter the value of n\n”);
scanf(“%d”,&n);
res=fact(n);
printf(“result=%d”,res);
}
The recursion refers to the process where a function calls itself either directly
or indirectly.
• There are two types:
– Direct recursion
– Indirect recursion

Direct Recursion:
it refers to a process where function calls itself directly as illustrated in
the figure below
Indirect Recursion:
It refers to a process where a function calls another function and that function calls back
the original function. The process of indirect recursion takes place as illustrated below:
0,1,1,2,3,5,8,13,.....

You might also like