Function to calculate the sum of two numbers 0 872

Function to calculate the sum of two numbers:

int sum(int, int); /* Function Prototype or Declaration*/

main( )

{

int a,b,ans;

printf(“Enter two numbers: “);

scanf(“%d %d”,&a,&b);

ans= sum(a,b); /*Function Call*/

printf(“ sum is %d”, ans);

getch( );

}

int sum( int x, int y) /*Function Definition or Process*/

{

int z;

z = x + y;

return(z);

}

Now let us see some of the features of this program:

1. The first statement is the declaration of the function which tells the compiler the name of the function and the data type of the argument passed.

2. The declaration is also called as prototype.

3. The declaration of a function is not necessary if the output type is an integer value. In some C compilers declaration is not required for all the function.

4. The function call is the way of using the function. A function is declared once, defined once but can be used a number of times in the same program.

When the compiler encounters the function call, the control of the program is transferred to the function definition the function is then executed line by line and a value is returned at the end.

5. At the end of the main program is the definition, which can also be called as process.

6. The function definition does not terminate with a semicolon.

7. A function may or may not return any value. Thus the presence of the return tatement is not necessary. When the return statement is encountered, the control is immediately passed back to the calling function.

8. While it is possible to pass any number of arguments to a function, the called statement returns only one value at a call.

The return statement can be used as: return; or return(value); The first return without any value, it acts much as the closing of the braces of the function definition.

9. A function may have more than one return statement. It can be used as: if (a!=0) return(a); else return(1);

10. All functions by default return int. But if the function has to return a particular type of data the type specifiers can be used along with the function name. long int fact(n)

11. If function main( ) calls a function sum( ) then main( ) is the calling function and sum( ) is called function.

Previous ArticleNext Article
Indian Mirchi will serve you taste in Indian news, general knowledge and information. Hope you like the story I write here and expect your points of discussion to improve them and give the best chili test out of the story. I will write tutorials also that helps students. In case you are looking for any specific information then share me in the comment.

Send this to a friend