Function in C
Function in C
A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling program So function in a C program has some properties discussed below.
Every function has a unique name. This name is used to call function from main()
perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc.
A function returns a value to the calling program. This is optional and depends upon
the task your function is going to accomplish. Suppose you want to just show few lines through function then it is not necessary to return a value. But if you are calculating area of rectangle and wanted to use result somewhere in program then you have to send back (return) value to the calling function. There are type two of function Built in function User defined function
Built in function C language is collection of various library functions. If you have written a program in C then it is evident that you have used Cs inbuilt functions. Printf, scanf, clrscr etc. all are Cs inbuilt functions. All the predefine functions are define in header files like input/output functions are define in stdio.h file, math functions are define in math.h User Define Function Function which are define by the programmer in the program is called user define functions. These function are declare , define and call by programmer in the program.
Function Definition Function definition is just writing logic of any function. Function definition has two part 1. function header 2. function body . function header is same a function declaration with out the semicolon that specify the returning type, function name, and function arguments. Function body contain the set of statements that are executed when the function is called. For example you have one function prototype in C program for adding two integer numbers (i.e. int add(int, int)) but along with prototype you also have to write logic how that function will behave (respect to above prototype; how you will utilize those two passed integer value and how you will return value) when you will call that function.
Structure of a Function
A general form of a C function looks like this: <return type> FunctionName (Argument1, Argument2, Argument3) { Statement1; Statement2; Statement3; } An example of function. int sum (int x, int y) { int result; result = x + y; return (result); }
a=10; b=15; c=add(a,b); printf("\n%d",c); getch(); } int add(int a, int b) { return a+b; }
Function call After declaring and defining a function it can be called form main function or other function. A function can be called more then one time. A function called may pass arguments. It may or may not accept return. Function call must be according to function declaration. Syntax : returntype functionname(list of actual arguments); Example of function calling in C
#include<stdio.h> #include<conio.h> void add(int x,int y) { int result; result = x+y; printf("Sum of %d and %d is %d.\n\n",x,y,result); } void main() { clrscr(); add(10,15); add(55,64); add(168,325); getch(); }
Program Output
Rule for user define functions Function declaration, function definition header must match type, sequence and number of arguments in function call and function definition must match if function is returning a value then function call must be assign to a variable having same type. function declaration can be local or global
Types of functions:
A function may belong to any one of the following categories: 1. Functions with no arguments and no return values. 2. Functions with arguments and no return values. 3. Functions with arguments and return values. 4. Functions that return multiple values. 5. Functions with no arguments and return values.
5. { 6. int z; 7. clrscr(); 8. z = add(952,321); 9. printf("Result %d.\n\n",add(30,55)); 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. printf("Result %d.\n\n",z); getch(); } int add(int x, int y) { int result; result = x+y; return(result); }
#include<stdio.h> #include<conio.h> int sum(); void main() { int z; clrscr(); z = add(); printf("Result %d.\n\n",z); getch(); } int add( ) { int result;
10.
11. 12. 13. 14. 15. 16. 17.
printf(a=%d b=%b,a,b); printf("Sum = %d, Sub = %d",p,q); getch(); } void calc(int x,int y, int *p, int *q) { p=x*y; q=x*y; printf(%d%d,x,y); }
18.
19.
int main() { int x=10, y=20; clrscr(); printf("Value of x = %d and y = %d.\n", x,y); callByValue(x, y); printf("\nCall By Value function call...\n"); printf("Value of x = %d and y = %d.\n", x,y); callByReference(&x, &y); printf("\nCall By Reference function call...\n"); printf("Value of x = %d and y = %d.\n", x,y); getch(); return 0; } void callByValue(int x, int y) { int temp; temp = x; x = y; y = temp; } void callByReference(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; }
In above call by value and call by reference C program example we have two functions; callByValue() and callByReference() to demonstrate respective logic. Lets start with callByValue() function, this function does swapping of variable using temp variable, we have to pass two integer type data as arguments to this function. The callByReference() function also does swapping of variable using temp variable, but we have to pass two integer type pointeras arguments of this function. To reduce confusion; I kept variable name and logic same in both function except some extra asterisk (*) in callByReference() function. We have declared two integer variable . We will pass those two variables to both function and will try to swap value stored in that. First we are calling function callByValue() and we are passing those two integer value. In line no. 15 we are displaying the value of variables (x and
y) after swapping. I know that swapping didnt happen when we called callByValue() function. In we are calling function callByReference () and here we are passing address ofthose two integer variable. In l we are displaying the value of variables (x and y). As you can see value of variables (x and y) is successfully swapped. You must be thinking why this swapping logic worked for callByReference() function but not for callByValue() function.
y = temp; printf("Value of x = %d and y = %d inside callByValue function.\n", x,y); } void callByReference(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; }
We have learnt different types function C language and now I am going to explain recursive function in C. A function is called recursive if a statement within body of that function calls the same function for example look at below code: void main() { printf(recursive function called.\n); main(); } When you will run this program it will print message recursive function called. indefinitely. If you are using Turbo C/C++ compiler then you need to press Ctrl + Break key to break this in definite loop.
10
#include<conio.h> int factorial(int); int factorial (int i) { int f; if(i==1) return 1; else f = i* factorial (i-1); return f; } void main() { int x; clrscr(); printf("Enter any number to calculate factorial :"); scanf("%d",&x); printf("\nFactorial : %d", factorial (x)); getch(); }
11
12