FUNCTIONS
Function:- It is a self contained block of statements and it can be used at several multiple times in a program but defined only once.
Library function or Predefined functions:- The functions which are in built with the C-compiler is called as library functions. Ex:- printf, scanf, getch(), clrscr(); etc.,
User defined function:- User can defined functions to do a task relevant to their programs. Such functions are called as user defined functions.
Any function has three things. They are…
1. Function declaration
2. Function definition
3. Function calling.
In case of pre-defined functions the function declaration is in header files, definition is in C Libraries and calling is in your source program.
But in case of user defined functions all the three things are in your source program.
Function declaration:-
Syntax:- Returntype func_name([Arg List]); Example:-Void test(); Int sum(int , int );
Function definition:- Syntax:- returntype func_name([Arg List]) { Body; }
Function calling:-
Syntax:- func_name([Arg List]);
The arguments which we given at the time of function declaration or definition are called arguments or formal arguments.
The arguments which are given at the time of function calling are called actual arguments or parameters.
void:- (Empty data type)
#include<stdio.h> #include<conio.h> void main() { void test( ); /declaration/ clrscr(); test( ); /calling/ getch(); } void test() /definition/ { printf(“welcome to c functions:”); }
Rules for creating and accessing user defined functions:-
- A function can be called by any number of times.
- A function may or may not receive arguments.
- A function may or may not return a value.
- If a function does not return any value the function return data type will be specified as “void”
- If a function returns a value only one value can be returned.
- We cannot specify any return data type, the function returns on integer value by default.
- The returning value must be return with a statement “return”.
- If a function returns a value, the execution of return statement should be last
- If a function returns a value, the returning value should match with the function return data type.
- A function is executed when a function is call by its name.
- Before a function call, function declaration or definition must and should
- A function definition may be placed before or after the main function.
- If a function call, function definition can be specified any where in the program
- If a function definition is specified before the function called then the function declaration is not necessary.
- The function definition should not be terminated with semicolon( ; )
Return:- Exits immediately from the currently execution of function to the calling rotated optionally returning a value.
Syntax:- return [<expression>];
Program: #include<stdio.h> #include<conio.h> void main() { int sum(int,int); int a,b,n; clrscr(); printf("Enter any two values:"); scanf("%d%d",&a,&b); n=sum(a,b); printf("Sum is %d",n); getch(); } int sum(int x,int y) { return x+y; }
Function Prototypes and category of functions:
A Function depending and whether arguments are present or not and a value is returned or not, may belong to one of the following categories.
- Category 1: Function with no arguments and no return value
- Category 2: Function with arguments and no return value
- Category 3: Function with arguments and return value.
- Category 4: Function with no arguments and return value.
- Function with no arguments and no return value:-
When a function has no arguments it does not receive any data from the calling function. Similarly when it does not return a value the calling function does not receive any data from the called function. In affect there is no data transfer between the calling function and the called function.
Program: #include<stdio.h> #include<conio.h> void main() { void sum(); /declaration;/ clrscr(); sum(); /calling;/ getch(); } void sum() /* definition*/ { int a,b; printf("Enter any two values:"); scanf("%d%d",&a,&b); printf("Sum is %d\n",a+b); }
2.Function with arguments and no return value:-
In this type the function has some arguments, it receive data from the calling function but it does not return any value. The calling function does not receive data from the called function. So there is one way data communication calling function and the called function.
Program: #include<stdio.h> #include<conio.h> void main() { void sum(int, int); /declaration;/ int a,b; clrscr(); printf("Enter any two values:"); scanf("%d%d",&a,&b); sum(a,b); /calling;/ getch(); } void sum(int x, int y) /* definition*/ { printf("Sum is %d\n",x+y); }
3.Function with arguments and return value:-
In this type the function has some arguments. It receives data from the calling function. Similarly it returns a value. The calling function receive data from the called function. So it is a two way data communication between the calling function and the called function.
Program: #include<stdio.h> #include<conio.h> void main() { int sum(int,int); int a,b,n; clrscr(); printf("Enter any two values:"); scanf("%d%d",&a,&b); n=sum(a,b); printf("Sum is %d",n); getch(); } int sum(int x,int y) { return x+y; }
4. Function with no arguments and return value:-
In this type the function has no arguments. It does not receive data from the calling function. But it returns a value the calling function receive data from the called function. So it is a one way data communication between called function and the calling function.
Program: #include<stdio.h> #include<conio.h> void main() { int sum(); /declaration;/ int s; clrscr(); s=sum(); /calling;/ printf("Sum is %d",s); getch(); } int sum() /* definition*/ { int a,b; printf("Enter any two values:"); scanf("%d%d",&a,&b); return a+b; }
CALL BY VALUE:
- In call by value method, the value of the variable is passed to the function as parameter.
- The value of the actual parameter can not be modified by formal parameter.
- Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter.
Note:
- Actual parameter – This is the argument which is used in function call.
- Formal parameter – This is the argument which is used in function definition
EXAMPLE PROGRAM FOR C FUNCTION (USING CALL BY VALUE):
- In this program, the values of the variables “m” and “n” are passed to the function “swap”.
- These values are copied to formal parameters “a” and “b” in swap function and used.
#include<stdio.h> // function prototype, also called function declaration void swap(int a, int b); int main() { int m = 22, n = 44; // calling swap function by value printf(" values before swap m = %d \nand n = %d", m, n); swap(m, n); } void swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; printf(" \nvalues after swap m = %d\n and n = %d", a, b); }
OUTPUT:
values before swap m = 22 and n = 44 values after swap m = 44 and n = 22 |
CALL BY REFERENCE:
- In call by reference method, the address of the variable is passed to the function as parameter.
- The value of the actual parameter can be modified by formal parameter.
- Same memory is used for both actual and formal parameters since only address is used by both parameters.
EXAMPLE PROGRAM FOR C FUNCTION (USING CALL BY REFERENCE):
- In this program, the address of the variables “m” and “n” are passed to the function “swap”.
- These values are not copied to formal parameters “a” and “b” in swap function.
- Because, they are just holding the address of those variables.
- This address is used to access and change the values of the variables.
#include<stdio.h>
// function prototype, also called function declaration
void swap(int *a, int *b);
int main()
{
int m = 22, n = 44;
// calling swap function by reference
printf(“values before swap m = %d \n and n = %d”,m,n);
swap(&m, &n);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf(“\n values after swap a = %d \nand b = %d”, *a, *b);
}
OUTPUT:
values before swap m = 22 and n = 44 values after swap a = 44 and b = 22 |