Tuesday, January 29, 2019

Function


Function: A function is defined as a self-contained block of statements that performs a particular task or job. The specific task is repeated each time the program calls the function. Function breaks large computing tasks into smaller ones. They work together to accomplish the goal of the whole program. Each program must contain one function named main ( ) where the program always begins executing.

Advantages of Function:
·       Manageability: It makes programs significantly easier to understand and maintain by breaking them up into easily manageable chunks. The code for each job or activity is placed in individual functions such that testing and debugging becomes easy and efficient.
·       Code reusability: A single function can be used multiple times by a single program from different places or different programs.
·       Non-redundant (non–repeated) programming: While using functions, the same function is called when needed. The repeated activity can be placed within a single function, which then accessed whenever it is needed.
·       Logical clarity: When a single program is decomposed into various well-defined functions, the main program consists of a series of function call rather than countless lines of code, so that the size of main program seems small and program becomes logically clear to understand.
·       Easy to divide the work to many different programmers: Different programmers working on one large project can divide the workload by writing different functions.

Library function: These are the function which are already written, compiled and placed in C Library and they do not required to be written by a programmer. The function’s name, its return type, their argument number and types have already defined.
Example: printf( ), scanf( ), getch( )

User-defined Function: These are the functions which are defined by user at the time of writing a program. The user has choice to choose its name, return type, arguments and their type.
Types of user-defined function are
a)     Functions without arguments and no return value
b)     Functions without arguments and  return value
c)     Functions with arguments and no return value
d)     Functions with arguments and no return value

Write a program using user defined function to calculate y raise to power x.[ HSEB2067]
#include<stdio.h>
#include<conio.h>
int power(int, int);
int main()
{
int y,x,p;
printf(" Enter values for y and x: ");
scanf("%d%d",&y,&x);
p=power(y,x);
printf("y raise to power x= %d",p);
getch();
}
int power(int y, int x)
{
int pw=1,i;
for(i=1;i<=x;i++)
pw=pw*y;
return pw;
}

Output: Enter values for y and x: 4 2
            y raise to power x= 16

Function Definition: The collection of program statements that describes the specific task to be done by the function is called function definition. It consists of the function header, which defines function’s name, its return type and its arguments list and a function body, which a block of code enclosed in parenthesis.
Syntax for function definition:
            return_type function_name(data type  variable 1,…… data type  variable n)
            {
                        statements
            }
The first line of the function definition is known as function declaration or header.
This is followed by function body which composed of the statements that make up the function, delimited by braces.

Function Prototype or Declaration: All the functions used inside a program must be declared. This process is called prototyping. It provides following information
ü  The name of the function.
ü  The type of the value returned by the function.
ü  The number and the type of arguments that must be supplied while calling the function.
 Syntax:
            return_type function_name (type1, type 2, type 3………….type n);
eg.
            int heading(int);
where, int------------------àreturn type
            heading------------àfunction name
            int-------------------àparameters
The prototype process is placed after header file and before the main function.
Note: Function prototype has semicolon at the end.

Write a program to find the sum of n integer numbers using function.
#include<stdio.h>
#include<conio.h>
int sum(int);
int main()
{
int n,ans;
printf("Enter any numbers: ");
scanf("%d",&n);
ans=sum(n);
printf("Sum of %d numbers= %d",n,ans);
getch();
 }
int sum(int n)
{
inti,s=0;
for(i=1;i<=n;i++)
 s=s+i;
return s;
}
Output: Enter any numbers: 4
Sum of 4 numbers= 10

Write a program to add two integer numbers using function. [ HSEB 2072]
#include<stdio.h>
#include<conio.h>
int sum(int,int);
int main()
{
int n1,n2,ans;
printf("Enter any numbers: ");
scanf("%d%d",&n1,&n2);
ans=sum(n1,n2);
printf("Sum of two numbers= %d",ans);
getch();
 }
int sum(int n1, int n2)
             {
   int num3;
   num3 = n1 + n2;
   return (num3);
}


1.     Write a program to multiply two integer number using functions. [2073]
#include<stdio.h>
#include<conio.h>
int mul(int,int);
int main()
{
int n1,n2,ans;
printf("Enter any numbers: ");
scanf("%d%d",&n1,&n2);
ans=mul(n1,n2);
printf("Sum of two numbers= %d",ans);
getch();
 }
int mul(int n1, int n2)
             {
   int num3;
   num3 = n1 * n2;
   return (num3);

}

Return and void statements of a function: Two types on the basis of return type.
Some of them return value from the called function and some do not (procedures).The procedure has void statement at the beginning if the function declaration/prototyping. The function that does not return any values can be explicitly defined as void.

Accessing a Function types of Accessing
Call by value
Call by reference
The values are passed to function as actual arguments.
The address is passed to function as actual arguments.
The argument which is called by value does not change in calling programming even it changes in called function.
The arguments which is called by reference changes in calling program also when it changes in called function.

 Write a program to calculate the factorial of a given number using function.
#include<stdio.h>
#include<conio.h>
int fact(int);
main()
{
int n,a;
printf("Enter any number: ");
scanf("%d",&n);
 a=fact(n);
printf("Factorial= %d",a);
getch();
 }
int fact(int n)
{
int i,f=1;
for(i=1;i<=n; i++)
 f=f*i;
return f;
}

Write a program to find out given number is odd or even using function. [2074]
#include<stdio.h>
#include<conio.h>
int even(int);
int main()
{
            int n;
            printf("enter any number");
            scanf("%d",&n);
            even(n);
            getch();
}
int even(int n)
{
            if(n%2==0)
            printf("The number is even");
            else
            printf("The number is odd");
}

Recursion: Any function that calls the same function during the execution is called recursion.
Syntax:
            return_type  function_name(data_typeparameter_passed)
            {
            block of code;
            function_name(new parameter_passed); //recursive call
}

Write a program to calculate the factorial of a given number using recursive function. ( HSEB2064,2068)
#include<stdio.h>
#include<conio.h>
int fact(int);
main()
{
int n,a;
printf("Enter any number: ");
scanf("%d",&n);
a=fact(n);
printf(" factorial= %d",a);
getch();
 }
int fact(int n)
{
if(n<=1)
return 1;
else
return(n*fact(n-1));
}

No comments:

Post a Comment