Summary

This document provides a comprehensive overview of functions in the C programming language. It delves into different types of functions, including pre-defined and user-defined functions, explaining their purpose and usage in C code. It includes examples and concepts of function calls and definitions.

Full Transcript

FUNCTION S Introduction ► Programs written by the programmer are sometime too large and complex, debugging and testing such program then becomes difficult. But if such large program are divided into subprograms and compiled separately then task becomes ea...

FUNCTION S Introduction ► Programs written by the programmer are sometime too large and complex, debugging and testing such program then becomes difficult. But if such large program are divided into subprograms and compiled separately then task becomes easy. C language provides support for such type of structuring using functions. ► Functions are the building blocks of C language. A number of statements grouped into a single logical unit is called as function. ► A function is a group of statements that together perform a specific well-defined task. Definition of the function: ► The function is a sub program or sub routine or self contained block of one or more executable statements, which performs a well defined task. ► A function is a small independent unit of a program. ► We have two types of functions. 1. Standard/ Pre Defined/ Library Functions. 2. User Defined Functions. Pre Defined Functions: ► These functions are pre defined set of statements. Their task is limited i.e., a function which is already exist in C header file is called as Standard or Pre defined or Library Function. ► In these functions the user cannot understand the internal working of the function. The user can only use these functions but cannot modify them. ► For example: printf(), scanf(), sqrt() and so on. If we want to use these functions in our programs we need to include the corresponding header file in our program. e.g the function sqrt() is defined in , getch() is defined in User Defined functions in C ► The functions defined by users according to the user requirements are called as user defined functions. In these functions the user can able to modify the function definition according to his or her requirement. The user has full scope on user defined functions. ► The user certainly understands the internal working of the function also. ► Need of User Defined Functions or Advantages If we want to perform a task repeatedly then it is not to write a particular block of program again and again. Keep the block of statements in user defined functions. The function defined can be used for any number of times to perform the task. Using function the large programs can be reduced to smaller programs. It is easy to find out errors. It also increases the readability to the compiler. ► Parts of User Defined Function or Steps for creating a user defined function Every user defined function must have the following components. 1. Function prototype or Function Declaration. 2. Function calling or caller or Function Call. 3. Function Definition or Called Function. Write a C program to illustrate the user defined functions in C #include #include int add(); //function Declaration main() { int c; clrscr(); c=add(); //function call printf("\nThe sum is %d",c); getch(); } int add() //function definition { int a,b,c; printf("\nEnter two numbers:"); scanf("%d%d",&a,&b); c=a+b; return(c); } 1. Function Proto type or function declaration It is the declaration portion of the user defined function. It tells to the compiler about the following information. Return type of the function Name of the function Number of arguments of the function Type of arguments. Syntax: Return_type function_name(argument_list); Example: int sum(int, int); We can place this function prototype within the main() method (local declaration) or outside of the main() method(global declaration). Function calling The function calling takes the responsibility of sending the data from main() function to sub function. The values that are entered in the calling function are send to called function. Syntax: Returned value= function_name(arguments list); Example: c=sum(a,b); Called Function This is the actual user defined function., it will get the data from the calling functions, and do the necessary processing as per the instructions written by the programmer and also returns or of may not return that data to the calling functions. Syntax: Return_type function_name(argument_list) Example: { int sum(int x,int y) //body of the method { } //Body of the function return 1; } Working of a Function ► Actual Parameter The arguments of calling function are called as Actual parameters. For example from the above structure the variables a,b are called as actual parameters ► Formal Parameters The arguments of called functions are called as Formal Parameters. For example from the above structure the variables x and y are called as Formal parameters ► Arguments list The argument list means the variable names enclosed within the parenthesis () and they must be separated by comma(,). By default every user defined function return an integer value. If we donot require any return value we have to use the keyword “void”. Types of user defined functions in C ► Depending on the return type of the method, the arguments of the method the user defined functions can be classified into four categories. They are 1. Function without return type and without arguments. 2. Function without return type and with arguments. 3. Function with return type and without arguments 4. Function with return type and with arguments. 1. A function without return type and without arguments In these functions neither the data is passed through the calling function nor the data is sent back from the called function. There is no data transfer between calling and called functions. The function is only executed and nothing is obtained. The function acts as independent. They read the data values and print the results in same blocks. These functions may be useful to print some message, draw a line and etc… Syntax: The following program illustrates the user defined function without return type and without arguments. #include #include void main() { void msg(); clrscr(); msg(); getch(); } void msg() { printf("Welcome to user defined functions"); } 2. A function without return type and with arguments In these functions the arguments are passed through the calling function. The called function operates on the received values. No result is sent back from called function to calling function. These functions are partially dependent on calling functions. #include #include void evenodd(int); void main() { int n; clrscr(); printf("\n Enter an integer:"); scanf("%d",&n); evenodd(n); getch(); } void evenodd(int x) { if(x%2==0) printf("%d is an even number",x); else printf("%d is an odd number",x); } 3.A function with return type and without arguments In these functions no arguments are passed through the calling functions. The called function returns a value to the calling functions. The called function is independent i.e., it reads values from the keyboard and returns the values. Here both calling and called functions are partially communicated with each other. #include #include float value(); void main() { clrscr(); printf("\nThe float value is %f",value()); getch(); } float value() { float f; printf("\nEnter a float value:"); scanf("%f",&f); return f; } 4.A function with arguments and with return type ► In such function the copy of the actual arguments are passed to the formal arguments. ► Value is written from callee to the function call. ► In these functions the data is transferred between calling and called functions i.e., the communication between the caller and callee is made. ► Syntax: #include int factorial(int x) #include { int fact=1,i; int factorial(int); if(x==0) void main() { return 1; { } int n,res; else { clrscr(); for(i=1;i

Use Quizgecko on...
Browser
Browser