Monday, March 2, 2020

Grade-12 (Function and Structure)

1.       What is function? Write its advantages.
Ans: A function is a self contained block of statements that perform some specific and well defined task. A function can be user or predefined (library) function.
Advantages of function
a.       Function increases reusability of program code.
b.      Program development is faster.
c.       Program debugging is faster and easier.
d.      Length of the program can be reduced.
e.      With function, it is easier to understand the logic involved in the program.
f.        Function reduces program complexity.
g.       Recursive call is possible through function.
h.      Function increases program readability.
2.    What are the differences between library function and user defined function. Write a program using user defined function to calculate y raise to power x.
        Differences between user defined function and library function:
Library function
User defined function
a. It is a predefined function in a header file or preprocessor directive.
b. Programmer can simply use this function by including respective header files.
c. The program using library function will be usually short as the programmer doesn’t have to define the function.
d. Program development time will be faster.
e. Example. Printf( ), getch( ), strlen( )
a. User defined function is not a predefined function. It is defined by the programmer according to the need.
b. Programmer has to declare, define and use this function by them.
c. The program using user defined function will be usually lengthy as the programmer has to define the function.
d. Program development time will be usually slower.
e. Example: Sum( ), area( ), Factorial( ), etc.


C Structures

Structure is a user-defined data type in C language which allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only.
Differences between array and structure

ARRAY
STRUCTURE
An array is a collection of variables of same data type.
A structure is a collection of variables of different data type.
type array_name[size];
struct sruct_name{
type element1;
type element1;
.
.
} variable1, variable2, . .;
Array elements are stored in contiguous memory location.
Structure elements may not be stored in a contiguous memory location.
Array elements are accessed by their index number.
Structure elements are accessed by their names.
Array declaration and element accessing operator is "[   ]" (square bracket).
Structure element accessing operator is "." (Dot operator).
Array name points to the first element in that array so, array name is a pointer.
Structure name does not point to the first element in that   structure so, structure name is not a pointer.
Objects (instances) of an array can not be created.
Structure objects (instance or structure variable) can be created.
Every element in array is of same size.
Every element in a structure is of different data type.
Bit filed can not be defined in an array.
Bit field can be defined in a structure.
There is no keyword to declare an array.
"struct" is a keyword used to declare the structure.
Arrays are not user-defined they are directly declared.
Structure is a user-defined datatype.

Write a program to read 100 different number from users, store them in an array, passes the array through function and find maximum number among them.

0 comments:

Post a Comment