C Programming | User Defined Functions

Qus. 01: What do you mean by user defined functions?

Ans. : User defined functions : C functions can be classified into two categories, namely, library functions and user-defined functions. The functions which are developed by user at the time of writing a program are called user defined functions. So, user defined functions are functions created and developed by user.
            Example : Here function01( ) is an user defined function.
main( )
{
=======
=======
function01( );
=======
}
function01( );
{
========
========
}

Qus. 02: Write the necessities or advantages of user defined functions. [not important]

Ans. : Necessity of user defined functions : When not using user defined functions, for a large program the tasks of debugging, compiling etc may become difficult in general. That’s why user defined functions are extremely necessary for complex programs. The necessities or advantages are as follows,

01. It facilitates top-down modular programming. In this programming style, the high level logic of the overall problem is solved first while the details of each lower-level function are addressed later

02. The length of a source program can be reduced by using functions at appropriate places.

03. It is easy to locate and isolate a faulty function for further investigations.

04. A function may be used by many other programs. This means that a C programmer can build on what others have already done, instead of starting all over again from scratch.

Qus 03: What do you mean by multifunction program?

Ans. : Multifunction program : A function is a self-contained block of code that performs a particular task. Once a function has been designed and packed, it can be treated as a ‘black box’ that takes some data from the main program and returns a value. Thus a program, which has been written using a number of functions, is treated as a multifunction program.


Ans. : Elements of user defined functions : In order to make use of a user-defined function, we need to establish three elements that are related to functions.
1.      Function definition
2.      Function call
3.      Function declaration
The function definition is an independent program module that is specially written to implement to the requirements of the function.
In order to use the function we need to invoke it at a required place in the program. This is known as the function call.
The program or a function that has called a function is referred to as the calling function or calling program. The calling program should declare any function that is to be used later in the program. This is known as the function declaration.


Ans. : Function Definition : The function definition is an independent program module that is specially written to implement to the requirements of the function. A function definition, also known as function implementation shall include the following elements
1.   Function name
2.   Function type
3.   List of parameters
4.   Local variable declarations
5.   Function statements
6.   A return statement
All the six elements are grouped into two parts; namely,
Ü  Function header (First three elements)
Ü Function body (Second three elements)

Function Header
The function header consists of three parts; function type, function name and list of parameter.
 
(a) Function Type
The function type specifies the type of value (like float or double) that the function is expected to return to the calling program. If the return type is not explicitly specified, C will assume that it is an integer type.
(b) Function name
The function name is any valid C identifier and therefore must follow the same rules of formation as other variable names in C. The name should be appropriate to the task performed by the function.

(c) List of Parameter
The parameter list declares the variables that will receive the data sent by the calling program. They serve as input data to the function to carry out the specified task.
            Example :
Float mul (float x, float y) {….}
t sum (int a, int b) {….}
Function body
The function body is enclosed in braces, contains three parts, in the order given below:
1.   Local variable declaration : Local variable declarations are statements that specify the variables needed by the function.
2.   Function Statements : Function statements are statements that perform the task of the function.
3.   Return Statements : A return statement is a statement that returns the value evaluated by the function to the calling program. If a function does not return any value, one can omit the return statement.

Qus. 06: Discuss about function call.

Ans. : Function call : In order to use functions user need to invoke it at a required place in the program. This is known as the function call. A function can be called by simply using the function name followed by a list of actual parameters, if any, enclosed in parentheses.
            Example : Here in the main() program the mul(10,5) function has been called.
main()
{
int y;
y=mul(10,5);                   /*Function Call*\
printf(“%d\n”,y);
}


Ans. : Function declaration : The program or a function that called a function is referred to as the calling function or calling program. The calling program should declare any function that is to be used later in the program. This is known as the function declaration.
            A function declaration consists of four parts. They are,
§  Function type
§  Function name
§  Parameter list
§  Terminating semicolon
They are coded in the following format :
          Funtion_type   function_name(parameter list);

Points to note :
§  The parameter list must be separated by commas.
§  If the function has no formal parameters, the list is written as void.
§  The return type is optional when the function returns int type data.
§  When the declared type does not match the types in the function definition compiler will produce an error.




Ans. : Parameter list : The parameter list contains declaration of variables separated by commas and surrounded by parentheses.
Float quadratic (int a, int b, int c){….}


Ans. : Prototype : The declaration of a function is known as function prototype. The function prototype is coded in the following format,
          Funtion_type   function_name(parameter list);


Ans. : Nesting of functions : In C, each function can contain one or more than one function in it. There is no limit as to how deeply functions can be nested. Consider the following example,
= = = = = = = = = = =
main()
{
function01();
}
function01()
{
function02();
}
function02()
{
...............;
}
= = = = = = = = = = =
 In the above example, The main() function contains function01(), the function01() contains function02 and so on. This is nesting of functions.

Qus. 11: What do you mean by recursion?

Ans. : Recursion : Recursion is a special case where a function calls itself. A very simple example of recursion is presented below,
main()
{
printf(“This is an example of recursion.\n”);
main();
}
When executed this program will produce an output which is something like this,
                                    This is an example of recursion.
This is an example of recursion.
                             This is an ex
Execution is terminated abruptly; otherwise the execution will continue indefinitely.

Qus. 12: Draw the flowchart of a multifunction program.


Ans. : Multifunction program : A program, which has been written using a number of functions, is treated as a multifunction program. The flowchart of the flow of control in a multifunction program is as follows,









ক্রেডিট : তন্ময় চক্রবর্তী

1 comments:

Tanmay Chakrabarty বলেছেন...

Well, you have mentioned the credit. Thats ok, but you could have told once that you are about to copy all my notes here as a book.

Its not a book, its my notes on C programming based on the book of C programming by E. Balaguruswamy.

Ok. Bye.

01. Overview of C

02. Constants, Variables & Data Types

03. Operators & Expressions

04. Managing I/O Operations

05. Decision Making & Branching

06. Decision Making & Looping

07. Arrays

08. User Defined Functions

Comment Please

মন্তব্য..

সবিস্তার সূচীপত্র
টেম্পলেট কাষ্টমাইজেশন - তরঙ্গ ইসলাম | তরঙ্গ ইসলাম