126x Filetype PDF File size 0.25 MB Source: simpletocompute.files.wordpress.com
UNIT – 4 CHAPTER 1 - FUNCTIONS Functions: Introduction – using functions – Function declaration/ prototype – Function definition – function call – return statement – Passing parameters – Scope of variables – Storage Classes – Recursive functions. 1. Introduction of Functions: Definition: A function is a block of code that performs a specific task. Types of Function: There are 2 types of functions in C programming. 1. Standard Library Functions and 2. User Defined Functions. 1. Standard Library Functions: The Standard Library functions are built – in functions in C programming to handle tasks such as I/O processing, mathematical calculations and string handling etc. Ex: printf( ) is a standard library function to send formatted output and it is defined by “stdio.h” header file. #include- printf( ), scanf( ), puts( ) and gets( ) etc. #include - clrscr( ) and getch( ) #include - strlen( ), strupr( ) and strlwr( ) etc. #include - sqrt( ), ceil( ) and floor( ) etc. 2. User Defined Functions: C language allows programmer to define functions, such functions created by the users are called User – Defined function. Depending upon the complexity and requirements the program, you can create as many user defined functions as you want. 2. Why we need functions (or) using of functions ? There are many benefits, so we need function for 1. Size reduced. 2. Complexity of entire program can be divided into simple sub – task. 3. Subprograms are easier to write, understand and debug. 4. Every subprogram compiled separately and loading them together. 5. Reduce the amount of work and development time. 6. Functions can be accessed repeatedly without redevelopment, which enables the reuse of the code (reusability). 3. Function Declarations / Prototypes: The general format for declaring a function is a data_type function_name(parameter list); Ex: int sum( int a, int b); Rules: 1. The parameter list must be separated by commas. 2. The parameter names do not need to be same in the prototype declaration and function definition. 3. The type must watch both in function prototype and function definition. 4. Function name must be meaningful name, function name is used to call for a program execution. 5. Use of parameter names in the declaration is optional. 6. Every function declaration should ended with semicolon (;) 7. A function can’t be declared within the body of another function. 8. A function having void, it does not return any value. Ex: void printf (void); int sum ( int a, int b); 4. Function Definition:- A function definition is also known as function implementation. It divided into 2 parts, 1. Function Header and 2. Function Body 1. Function Header: The function header consist of three parts, function type, function name and formal parameter list. It does not have semicolon at the end. 2. Function Body: The function body consists of three parts, local variable declaration, function statement and return statement (to return the value). The functions always return one variable value(s). Syntax: return_type function_name ( type parameter1, type parameter2,….) { // body of the function } Variables in function definition is called as parameter Ex: int sum ( int a, int b) { int c; c = a + b; return c; } 5. Function Call : A function call can be called by simply using the function name followed by a list of actual arguments. The variable in the function call are called as actual arguments. Syntax: Variable_name = function_name ( aruguments ); Example: c = sum ( a , b); You can also pass constant values also. i.e c = sum (10, 20) Ex:- #include #include int sum (int a, int b); void main( ) Sample Output: { Enter n1: 10 int n1, n2, total; Enter n2: 20 clrscr( ); Total = 30 printf(“Enter n1 :”); scanf(“%d”,&n1); printf(“Enter n2:”); scanf(“%d”,&n2); total = sum(n1,n2); printf(“Total = %d”, total); getch(); } int sum ( int a, int b) { int result; result = a + b; return result; }
no reviews yet
Please Login to review.