Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.

Similar presentations


Presentation on theme: "Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions."— Presentation transcript:

1 Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.  Every C program must have a main function to indicate where the program has to begin its executions.

2 Advantages:-  By using functions we can develop an application in module format  By using functions we can keep track of what logic we are implementing  The basic purpose of functions is code reusability  When we are developing an application in module format, then easily we can debug the program

3  A C program is a combination of functions  Execution process start from main() and ends with main() only  When we are working with functions, functions can be implemented randomly  From any function we call any other functions  When we are calling a function which is defined later, for avoiding the compilation error we required to go for forward declaration i.e function prototype is required

4 Concepts of functions  Function Declaration or Function Prototype.  Function Definition.  Function Body  Function Arguments.  Function Call.  Function Return Type.

5 Function Declaration  A function declaration can appear outside of all functions  A function declaration can also appear inside function main.  All functions know about other declarations and can call these functions Syntax: Return_type function_name (type,type,…); 1.Return_type specifies the data type of the value in the return statement. 2.A Function can return any data type; if there is no return value, the keyword void is placed before the function name. 3.The function declaration terminates with a semicolon 4.By default return type of function is int

6 Function Definition:  The function definition is similar to the function declaration but does not have the semicolon.  The first line of the function is called a function declarator. This is followed by the function body. It is composed of the statements that make up the function, delimited by braces.  The declarator and declaration must use the same function name, number of arguments, argument types, and the return type.  No function definition allowed within a function definition  If the functions are defined before they are called, then the declarations are unnecessary.

7 Function Call  A function is a dormant entity, which comes to life when a call is made to the function. A function call is specified by the function name followed by the values of the parameters enclosed within parentheses, terminated by a semicolon.  When the compiler encounters a function call, the control is transferred to the function. Then the function executed line by line. Eg:- f1(); f1(10); f1(10,20);

8 Function Return Type:  Function in C may or may not have return value  If a function does not return a value, the return type in the function definition and declaration is specified as void. Otherwise, the return type is specified as a valid data type. There may be multiple return statements.  By default return type is integer.

9 Syntax for creating a function return_type function_name(parameters) { statement_block; ------------------ ------------------ function body ------------------ return statement; }

10  According to the syntax specifying return type, parameters and return statement are optional  All the rules of variable declarations required to applied for function name also

11 In C programming language functions are classified into two types 1.Library Functions or Built-in Functions or Predefined Functions 2. User Defined Functions

12 1. Library Functions or Predefined Functions:-  They all are pre implemented set of functions which is available along with compiler  The implementation part of library functions are available in.LIB or.OBJ file .LIB or.OBJ files contain precompiled code  When we are working with predefined functions for avoiding the compilation error, we require to include header files into our application .h file doesn’t provide any implementation part of predefined function, it provides only forward declaration i.e prototype of the function

13 Limitations of Library functions or predefined funtions  All predefined Function contains limited task only  As a programmer we shouldn’t have any control over the predefined functions  It is not possible to alter or modify the behaviour of any predefined functions For eg:- printf(), clrscr(), scanf(), pow(), sqrt() etc

14 2.User Defined Functions:-  As per the client or project requirement what function we are implementing it is called as user defined functions.  All user defined functions are project specific functions only.  As a programmer we are having full control on user defined functions  It is possible to modify or alter the behaviour of any user defined functions if it required

15 In C programming language user defined functions are classified into 4 types:- 1. Function with no arguments and no return type 2. Function with arguments and no return type 3. Function with no argument and one return return value 4.Function with argument and one return type

16 . Qus:-What is the use of functions in C? Ans:-It is possible to code any program in main() function only, but the program become to large and complex and as a result the task of debugging, testing and maintaining becomes difficult, So to avoid this complex a program is divided into functional parts, by which we can independently code a program and later we can combined into a single unit. These independently coded programs are called as subprograms that are easier to understand,debug and test. In C such subprograms are called as Functions.

17 Previous year questions:- 1)Distinguish between user defined and built-in functions? 2)What is meant by function prototype? Give an example of function prototype 3)What are the commanly used input functions in C? How are they accessed? 4)What is function Parameter ? Explain different types of parameters in C functions.


Download ppt "Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions."

Similar presentations


Ads by Google