Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 3 P4 Functions

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

Module 6

(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

1 Function Declare (Before main())


2 Function Calling (With in the main() )
3 Function Definition (After the main() )
⚫ A function declaration tells the compiler about a
function's name, return type, and parameters.
⚫ A function definition provides the actual body of the
function.
⚫ Function name should be declare before the main
program.
⚫ The function definition means writing an actual code for a
function which have a specific and identifiable task.
⚫ Suppose we have declare a function sum() which find the
sum of entered number then we have to write a set of
instruction to do sum in the definition. And In order to use
this function we need to invoke it at a required place in
the program .this is known as function calling.
⚫Prototype of a Function
(How functions are declare)

The general form of a function definition in C


programming language is as follows −

return_type function_name( parameter list )


{
body of the function
}
⚫ A function definition in C programming consists of
a function header and a function body. Here are all the
parts of a function −
⚫ Return Type − A function may return a value.
The return_type is the data type of the value the function
returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is
the keyword void.
⚫ Function Name − This is the actual name of the function.
The function name and the parameter list together
constitute the function signature.
⚫ Parameters − A parameter is like a placeholder. When a
function is invoked, you pass a value to the parameter. This
value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a
function may contain no parameters.
⚫ Function Body − The function body contains a collection
of statements that define what the function does.
Declaration of function
# include<stdio.h>
int fun(); // function declaration
int main ()
{
int c = fun(); // function calling
printf(“%d”,c);
}
int fun() // function definition
{
return 10;
}
Function Example
#include<stdio.h>
void Name();
void main ()
{
printf("Hello ");
Name();
}
void Name()
{
printf(“World");
}
Output- Hello World
Function Example
#include<stdio.h>
void sum(); // Function Declare
void main()
{
printf("calculate the sum of two numbers:");
sum(); // Function Call
}

void sum() // Function Definition


{
int a,b;
printf("Enter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
⚫ Output-
Function Category
⚫ Function without Return type without arguments
void sum()
⚫ Function without Return type with arguments
void sum (int ,int)
⚫ Function with Return type without arguments
int sum()
⚫ Function with Return type with arguments
int sum( int , int)
⚫ Here Return type works with respect to output and
Parameters work with respect to Input.
⚫ For transfer input values from main program to function
definition we use Parameters and For transfer output values
from function definition to main program we use return
types.
⚫1 Return type always related to the output of our
program . if the function is without return type
means function definition does not return any
value to the function calling. And if the function is
with return type means the function definition
returns the computed value to the calling
functions.
⚫2 Arguments always related to the input of our
program. if the function is declare without
arguments means the function definition does not
receive any data from the main program through
function calling. The data is taken in the definition
from the user and if the function is declare with
arguments the function definition receive the data
from the main program and process on that data.
⚫ Arguments and Parameters

⚫ An argument is referred to the values that are passed within a


function when the function is called. These values are generally the
source of the function that require the arguments during the process of
execution. These values are assigned to the variables in the definition
of the function that is called. The type of the values passed in the
function is the same as that of the variables defined in the function
definition. These are also called Actual arguments or Actual
Parameters.

⚫ 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

⚫ In the function the arguments are passed to


the function and their values are copied in the
corresponding function definition . Means The
values that are passes in the calling of
function as an Arguments are directly copied
in the definition of the calling function as a
parameter . There are two techniques for
passing the parameters:

⚫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

⚫ Call by Reference :The Address are passed (Original


values are not copied, Address is copied)
⚫ In this method the actual values are not passed ,instead
their addresses are passed to the calling function , Here
no values are copied as the memory location themselves
are referenced. If any changes are made to the values in
the definition (parameters) then the original values will
get changed with in the calling function.
Recursion (Recursive Function)
⚫A Function calls itself again and again until a
specific condition is called recursion.
Recursion is a technique that defines a
function in terms of itself.
⚫That is a function which perform a particular
task is repeatedly calling itself .There must be
an exclusive stopping condition in a recursive
function. otherwise the function will not be
terminated its enter in the infinite loop.
⚫if a program allows you to call a function
inside the same function, then it is called a
recursive call of the function.
⚫There are some advantages of recursion
⚫By using Recursion we call a function continuously
until to a specific condition
⚫Through Recursion one can Solve problems in easy
way while its iterative solution is very big and
complex. Ex : tower of Hanoi
⚫The recursion is very flexible in data structure like
stacks, queues, linked list and quick sort.
⚫Using recursion, the length of the program can be
reduced.
⚫Using recursion we can avoid unnecessary calling
of functions.
⚫Recursive functions can be effectively used to
solve problems where the solution is expressed in
terms of applying the same solution.
WAP find the Factorial of any Numbers Using Recursion
#include <stdio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number whose factorial you want to calculate?");

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;
}

You might also like