CTUC101 Introduction to Programming Unit 5 PDF
Document Details
Uploaded by HardWorkingSandDune
CHARUSAT
Tags
Summary
This document introduces user-defined functions in C programming. It explains the need for functions, various benefits of using functions, and the concept of local data. The structure of a user-defined function, including a detailed outline of the header and body, is also given.
Full Transcript
B.Sc. IT Semester - I CTUC101 Introduction to Programming Unit-5 Introduction to Functions Need for User Defined Functions First, it makes programs easier to understand and maintain by breaking up a program into easily manageabl...
B.Sc. IT Semester - I CTUC101 Introduction to Programming Unit-5 Introduction to Functions Need for User Defined Functions First, it makes programs easier to understand and maintain by breaking up a program into easily manageable chunks. Secondly, the main program can consist of a series of function calls rather than countless lines of code. It can be executed as many times as necessary from different points in the main program. The third benefit is that well written functions may be reused in multiple programs. Fourthly, functions can be used to protect data. This is related with the concept of local data. Local data is the data described within a function. They are available only within a function when the function is being executed. The fifth benefit of using functions is that different programmers working on one large project can divide the workload by writing different functions. As we know main is a special function in C. Every program must have a main to indicate where the program has to begin its execution. The program may become too large and complex and as a result the task of debugging, testing and maintaining becomes difficult. If a program is divided into functional parts, then each part may be independently coded and later combined into a single unit. The independently coded programs are called subprograms that are much easier to understand, debug, test and maintain. In C, such programs are referred to as functions. 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 some value. The inner details of the operation are invisible to the rest of the program. All that the program knows about the function is: What Goes In and What Come Out. void printline(void); void main( ) { printline( ); printf(“This is the use of C function”); printline( ); } void printline (void) 1 { int i; for(i=1; i