Define functions. Write the advantages of function with program example.
A function is basically a block of statements that performs a particular task. Suppose a task needs to be performed continuously on many data at different points of time, like one at the beginning of the program and one at the end of the program, so instead of writing the same piece of code twice, a person can simply write it in a function and call it twice. And after the execution of any function block, the control always comes back to the main() function.
Here are several advantages of using functions in your code:
For eg:
In the following program, the function can be called / reused many times instead of having to write the same codes again and again.
#include<stdio.h> int sum(int,int); // function prototype int main() { int a,b,s; printf("Enter two integers:"); scanf("%d%d",&a,&b); s=sum(a,b); // fucntion call printf("Sum = %d",s); return 0; } int sum(int x,int y) // function definition { return (x+y); // return statement }Structures in Cis a user-defined data type available in C that allows to combining of data items of different kinds. Structures are used to represent a record.
Defining a structure:To define astructure, you must use thestructstatement. The struct statement defines a newdata type, with more than or equal to one member. The format of the struct statement is as follows:
struct [structure name] { member definition; member definition; ... member definition; }; (OR) struct [structure name] { member...