Unit 3 P4 Functions
Unit 3 P4 Functions
Unit 3 P4 Functions
(Functions )
Modular programming (functional
programming)
⚫ Modular programming (functional programming) is the process
of subdividing a computer program into separate sub-programs.
⚫ A module is a separate software component. It can often be
used in a variety of applications and functions with other
components of the system.
⚫ Similar functions are grouped in the same unit of programming
code and separate functions are developed as separate units of
code so that the code can be reused by other applications.
⚫ If a program is lengthy it becomes very difficult for the
programmer to handle it. Normally Larger programs are more
errors and its difficult to correct there errors. Therefore such
programmes should be broken down into a number of small
units called module or functions .The function or modules is a
set of instructions which are to be accessed repeatedly from
several different places within a program and call whenever
necessary .This avoids rewriting of functions on every access. its
also called reusability.
Advantages of modular programming:
⚫Fewer bug because each set of
programming commands is shorter
⚫Algorithm is more easily understood
⚫Programmers can use their expertise on
particular techniques
⚫Testing can be more thorough on each of
the modules
⚫All of which saves time and means the
finished program can be completed more
quickly
⚫ A function is a module or block of program code which
Functions
deals with a particular task. Making functions is a way
of isolating one block of code from other independent
blocks of code.
⚫ Functions serve two purposes.
⚫ They allow a programmer to say: `this piece of code
does a specific job which stands by itself and should not
be mixed up with anyting else',
⚫ Second they make a block of code reusable since a
function can be reused in many different contexts
without repeating parts of the program text.
⚫ A function can take a number of parameters, do
required processing and then return a value. There may
be a function which does not return any value.
⚫ You already have seen couple of built-in functions like
printf(); Similar way you can define your own functions
in C language.
Types of function in C
programming
⚫ System Defined Function
⚫ User Defined Function
⚫ The system defined function are also called library function
or built in function .All system define function are predefined in
the C library. And they can access directly from the library .
⚫ Like sqrt(),printf(),gets() etc. But most of the application
programmer needs some functions that are not in the
library .these function must be written by the programmer is
called user define function.
⚫ C provides programmer to define their own function according
to their requirement known as user defined functions.
Suppose, a programmer wants to find factorial of a number and
check whether it is prime or not in same program. Then, he/she
can create two separate user-defined functions in that program:
one for finding factorial and other for checking whether it is
prime or not.
Elements of user defined
function
⚫ In C programming the function name and there type must
be declared and defined before they are used in the
program.
⚫ Functions are basically special type of programs ,which
perform a specific task ,but don’t work independently as
main() function.
⚫ They get executed only when some other program calls
them. if required they receive data from the program and
may or may not return the data in to the program.
⚫ Three Necessary Condition for functions
⚫ Parameters
⚫ The parameter is referred to as the variables that are defined during a
function declaration or definition. These variables are used to receive
the arguments that are passed during a function call. These
parameters within the function prototype are used during the execution
of the function for which it is defined. These are also called Formal
arguments or Formal Parameters.
The following are the points to be remembered
while using arguments and parameters
⚫1 The number of arguments should be equal
to the number of parameters.
⚫2 There must be one to one mapping
between arguments and parameters.
⚫3 They should be in the same order and have
the same data type
⚫4 Same variables or different variable can be
used as arguments and parameters
#include <stdio.h>
int sum(int, int);
void main()
{
int num1 = 10, num2 = 20, res;
res = sum(num1, num2);
printf("The summation is %d", res);
}
int sum(int a, int b)
{
return a + b;
}
Here num1 & num2 as ARGUMENTS. and a,b are the parameters
⚫Parameter passing Techniques
⚫Call by Value
⚫Call By Reference
⚫ Call by Value : Actual Values are passed (Original values
are copied)
⚫ In this Method the values of the Arguments are passed
from a calling function to the definition ,the actual
values are copied into the definition of that function .if
any changes are made to the values in the definition
(parameters) there will not be any change in the original
values within the calling function
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if ( n == 1)
return 1;
else
return n*fact(n-1);
}
Result is transfer on
the last Iteration so its
tail Recursion
WAP find Sum of Natural Numbers Using Recursion
#include <stdio.h>
int sum(int n);
void main()
{
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum = %d", result);
}
int sum(int n)
{
if (n != 0)
return n + sum(n-1);
else return n;
}
find the nth term of the Fibonacci series Using Recursion
#include<stdio.h>
int f(int);
void main()
{
int n, m= 0, i;
printf("Enter Total terms:n");
scanf("%d", &n);
printf("Fibonacci series terms are:n");
for(i = 1; i <= n; i++)
{
printf("%d", f(m));
m++;
} }
int f(int n)
{
if(n == 0 || n == 1)
return n;
else
return(f(n-1) + f(n-2));
}
n=(n-2)+(n-1)
0,1,1,2,3,5,8,13,21…
find the nth term of the Fibonacci
series Using Recursion
#include<stdio.h>
void printFibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
int main(){
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
return 0;
}