Friday, 28 February 2014

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.

Return statementItis 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.

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;elsereturn(a*a*a)}OutputEnter value of x:5Cube of x is 125

Local and global variableLocal 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);}Outputa=10,b=20a=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//

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.Syntaxfunctionname(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//
}