1. Functions
A function is a block of statements which performs a particular task, je.g.printf is a function that is used to display anything on computer screen, scanf is another function that is used to take input from the user. Each program has a main function which performs the tasks programmed by the user. Similarly, we can write other functions and use them multiple times.
Types of Functions
There are basically two types of functions:
1) Built-inFunctions
2) User Defined Functions
Built-in Functions
The functions which are available in C Standard Library are called built-in functions. These functions perform commonly used mathematical calculations, string operations, input/output operations etc. For example, printf and scanf are built-in functions.
User Defined Functions
The functions which are defined by a programmer are called user-defined functions. In this chapter we will learn how to write user defined functions.
Advantages of function.
Function provide us several advantages.
1) Reusability:
Functions provide reusability of code. It means that whenever we need to use the functionality provided by the function, we just call the function. We do not need to write the same set of statements again and again.
1)Separation of tasks:
Functions allow us to separate the code of one task from the code of other tasks. If we have a problem in one function, then we do not need to check the whole program for removing the problem. We just need to focus at one single function.
3) Handling the complexity of the problem:
If we write the whole program as a single procedure, management of the program becomes difficult. Functions divide the program into smaller units, and thus reduce the complexity of the problem.
4) Readability: Dividing the program into multiple functions, improves the readability of the program.
Signature of a Function
A function is a block of statements that gets some inputs and provides some output. Inputs of a function are called parameters of the function, and output of the function is called its return value. A function can have multiple parameters, but it cannot return more than one values.
Function signature is used to define the inputs and output of a function. The general structure of a function signature is as follows:
return_type function_name(data_ type 1,data type 2,.........,data_type N
0 Comments