A function is a self contained block which design and execute separately.
C support two types of function.For ex:- sqrt(), power(),abs()….etc.
For ex:- void show() , int max(int a, int h) { { //statement //statement } }
Syntax function_name (parameterlist) { //function body }
function_name (parameterlist) EX: void show(); int max (int a,int b); // program to draw a line. #include<stdio.h> #include<conio.h> void drawline(); //function declaretion main() { drawline(); //function call getch(); } void drawline() //function defination { int i; clrscr(); for(i=1;i<=80;i++) { printf("-"); } }o/p
--------------------------------------------------------------------
Call by value means sending the values of the arguments to functions. When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function. Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of an argument to a function is known as passing by value or call by value.
EX: WAP for call by value#include<conio.h> #include<stdio.h> #include int swap(int,int); main() { int a,b; clrscr(); printf("\nEnter Value of a and b "); scanf("%d%d",&a,&b); printf("\n Before Printing "); printf("\na = %d and b = %d",a,b); swap(a,b); getch(); } int swap(int a,int b) { int temp; temp=a; a=b; b=temp; printf("\n After Printing "); printf("\na = %d and b = %d",a,b); }o/p
Enter a value of a and b 2 3 Before printing a=2 and b=3 After printing a=3 and b=2
Call by reference means sending the addresses of the arguments to the called function. In this method the addresses of actual arguments in the calling function are copied into formal arguments of the called functions. Thus using these addresses we would have an access to the actual arguments and hence we would be able to manipulate them. Using a call by reference intelligently, it is possible to make a function return more than one value at a time, which involves the study of pointer.
EX: WAP for call by reference#include<conio.h> #include<stdio.h> #include int swap(int*,int*); main() { int a,b; clrscr(); printf("\nEnter Value of a and b "); scanf("%d%d",&a,&b); printf("\n Before Printing "); printf("\na = %d and b = %d",a,b); swap(&a,&b); printf("\n After Printing "); printf("\na = %d and b = %d",a,b); getch(); } int swap(int *q,int *p) { int temp; temp=*q; *q=*p; *p=temp; }o/p
Enter a value of a and b 2 3 Before printing a=2 and b=3 After printing a=3 and b=2
A function which called itself repeatedly till condition is satisfied.
Syntax:function_name(parameterlist) { //statement } Function_name(argument_list) { //statement }EX: WAP to find factorial by using recursion
#include<stdio.h> #include<conio.h> int factorial(int); main() { int fact,n; clrscr(); printf("\nEnter any Number "); scanf("%d",&n); fact=factorial(n); printf("\n Factorial of %d is %d",n,fact); getch(); } int factorial(int n) { int f=1; if(n==1) return 1; else f=n*factorial(n-1); return f; }o/p
Enter any number 5 Factorial of 5 is 120