A function is a reusable piece of code which is used again and again. Functions in C can be categorised into two types
1. Library function
2. User Defined functions
1. Library functions are predefined functions which are copied into the system at the time of C installation(copy). Examples: clrscr(), getch(), sqrt(), pow() etc
2. A user defined function is a function whose prototype and definition are defined by the user according to his requirements.
To create a function and to use we need
a. function prototype
b. function call
c. function definition
A function prototype tells about the function return type and the arguments taken by the function.
syntax: return type function_name(arguments).
example: int add(int,int); /* add is a function name which accepts two integer arguments */
A function call is a call to a function called from the main function.
Syntax: return type function_name(arguments);
int a=add(4,5);
Function definition is a piece of code to be executed when a function call is made to it.
Syntax: return type function_name(arguments list)
{
list of statement to be executed
return statement
}
Example: int add(int c,int d)
{
return c+d;
}
Functions can be implemented in C using one of the following four methods
1. no argument and no return type
2. with arguments and with return type
3. no argument and with return type
4. with argument and no return type
1. using no argument and no return type:
Under this method a function does not accepts any argument from the calling function and will not return any thing to the the calling function. (void)
/* C program to add two number using no argument and no return type method */
void add(void);
main()
{
add();
}
void add(void)
{
int a,b;
printf("\n enter any two values");
scanf("%d%d".&a,&b);
printf("addition of %d and %dis %d",a,b,a+b);
}
OUTPUT:
Enter any two values
5 4
addition of 5 and 4 is 9
2. With arguments and return type:
Under this method a function accepts arguments but does not return any value to a calling function.
/* C program to add two number using with argument and with return type method */
int add(int,int); /* add function accepts two integer arguments */
main()
{
int a,b,c;
printf("\n enter any two values");
scanf("%d%d".&a,&b);
c=add(a,b); /* call to add function by passing two integer arguments */
printf("\n addition of %d and %d is %d",a,b,c);
}
int add(int c,int d) /* a value is copied to c and b value is copied to d respectively */
{
return c+d;
}
OUTPUT:
Enter any two values
5 4
addition of 5 and 4 is 9
3. no arguments and with return type:
Under this method a function will not accept any arguments and returns value to a calling function.
/* C program to add two number using no argument and with return type method */
int add(); /* add function will not accept any value but returns integer value*/
main()
{
int c;
c=add();
printf("\n addition is %d",c);
}
int add() /* a value is copied to c and b value is copied to d respectively */
{
printf("\n enter any two values");
scanf("%d%d".&a,&b);
return a+b;
}
OUTPUT:
Enter any two values
5 4
addition of is 9
4. with argument and no return type:
Under this method a function does not accepts any argument from the calling function and will not return any thing to the the calling function. (void)
/* C program to add two number using with argument and no return type method */
void add(int,int);
main()
{
int a,b;
printf("\n enter any two values");
scanf("%d%d".&a,&b);
add(a,b);
}
void add(int c,int d)
{
printf("addition of %d and %dis %d",c,d,c+d);
}
OUTPUT:
Enter any two values
5 4
addition of 5 and 4 is 9
call by value and call by reference
Functions in c, recursive functions, no argument and no return type, call by value, call by reference, with arguments and with return types, with arguments and no return types and no return type and with arguments , call by value, call by reference, recursive functions, tower of hanoi, gcd using recursion and non recursive function , factorial using recursion and non recursive functions, fibonacci using recursive and non recursive functions.
Enter any two values
5 4
addition of 5 and 4 is 9
call by value and call by reference
Functions in c, recursive functions, no argument and no return type, call by value, call by reference, with arguments and with return types, with arguments and no return types and no return type and with arguments , call by value, call by reference, recursive functions, tower of hanoi, gcd using recursion and non recursive function , factorial using recursion and non recursive functions, fibonacci using recursive and non recursive functions.
No comments:
Post a Comment