Friday 28 February 2014

Explain types of function with example.


Types of function

1)without arguments and return values-Here neither the data is passed from calling function nor the data is sent back from called function.
The function is executed,they print result in same block.Such type is used for printing messages,drwa a line or split line etc.

Write an example of function without argument and return values.

#include<stdio.h>
#include<conio.h>
void main()
{
    void a(),b(),c();
    a();   //Here main() function calls 3 functions//
    b();
    c();
}

void a()
{
    printf("\n A");
}
void b()
{
    printf("\n B");
}
void c()
{
    printf("\n C");
}
Output
ABC

2)with arguments but without return values-The arguments are passed to the called function,but no result is sent back.

write an example of function with arguments and no return values.

#include<stdio.h>
#include<conio.h>
main()
{
    int datee(int,int,int);
    int d,m,y;
    clrscr();
    printf("Enter date:");
    scanf("%d \t %d \t %d",&d,&m,&y);
    datee(d,m,y);
    return 0;
}
datee(int x,int y,int z)
{
    printf("Date=%d/%d/%d",x,y,z);
}
Output
Enter date:11 2 2013
Date=11/2/2013

3)with arguments and return values-The arguments are passed to called fucntion and result is sent back to calling function.

write an example of function with arguments and return value.

#include<stdio.h>
#include<conio.h>
main()
{
    int datee(int,int,int);
    int d,m,y;
    clrscr();
    printf("Enter date:");
    scanf("%d \t %d \t %d",&d,&m,&y);
    t=datee(d,m,y);
    printf("Tomorrow=%d/%d/%d",t,m,y);
    return 0;
}
datee(int x,int y,int z)
{
    printf("Today=%d/%d/%d",x,y,z);
    return(++x);
}
Output
Enter date: 12 2 2013
Today=12/2/2013
Tomorrow=13/2/2013

4)without arguments and with return values-No arguments are passed to called function,but called function returns values.
It reads values from keyboard.

write an example of function without arguments and with return values.

#include<stdio.h>
#include<conio.h>
main()
{
    int sum,s
    clrscr();
    s=sum();
    printf("\n Sum=%d",s);
}
sum()
{
    int x,y,z;
    printf("\n Enter 3 values:");
    scanf("%d %d %d",&x,&y,&z);
    return (x+y+z);
}
Output
Enter 3 values: 3 4 5
Sum=12

Explain return statement with example.

Return statement
Itis used to return value to calling function.
It is used for exit from called function to calling function.
When it is executed it always returns 1.
Absence of return statement indicates no value is returned.Such functions are called void.
Next statement following return statement willnot be executed.

The return statement can be used in different ways-

return;
It returns always 1   

return(expression);
example-
return(a+b+C)
if such a statement is executed then expression within parenthesis is first solved and result obtained is returned.

Multiple return statement within a function-
example-
if(a>b)
return a;
else
return b;

return(&p)-It returns address of the variable.
return(*p)-It returns value of variable through pointer.
return NULL-It return NULL value.

Write a program to show return statement in different ways.
#include<stdio.h>
#include<stdio.h>
main()
int pass(int);
int x,y;
clrscr();
printf("Enter value of x:");
scanf("%d",&x);
y=pass(x);
switch(y)
{
case 1:
printf("Returned value is %d",y);
break;
default:
printf("Cube of x is %d",y);
}
return NULL;
}

pass(a)
{
if(a==0)
return;
else
return(a*a*a)
}
Output
Enter value of x:5
Cube of x is 125

Use of Local and global variable in function with example

Local and global variable
Local variable is defined within the body of the fucntion.Other function cannot access these variables.
Global variables-These are defined outside main () fucntion.Mulitiple functions can use them.

Write a program to show local variables.
#include<stdio.h>
#include<conio.h>
main()
{
    int a=10,b=20;
    clrcsr();
    printf("a=%d , b=%d",a,b);
}
func()
{
    int a=20,b=10;
    printf("a=%d b=%d",a,b);
}
Output
a=10,b=20
a=20,b=10
//here variable a and b are defined in both functions,There effect is only within the function in which they are defined//

Write a program to show global variables.
#include<stdio.h>
#include<conio.h>
int a =10,b=10;
main()
{
    clrcsr();
    printf("a=%d , b=%d",a,b);
    func();
    printf("a=%d , b=%d",a,b);
}
func()
{
   
    a++;   
    b--;
}
Output
a=10  ,b=10
a=11  ,b=9

Explain function with example.

Function-A function is a sub block with one or two statments which perform a task when called.Whenever a fucntion is called control passes to called function from calling function.Control returns back to calling function only when execution is done.The values of actual arguments are passed to formal arguments by calling function.
The function performs on formal arguments.
Syntax
functionname(arguments)
    {
    local variable declaration;
    stmt;
    return value;
    }
Write a program to show how a function is called.
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int x=1,y=2,z;
z=add(x,y); //calling function with actual arguments x,y//
printf("z=%d",z);
}
add(a,b)
{
return(a+b);//called function with formal arguments a,b//
}