Sie sind auf Seite 1von 8

What is a Function?

A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required. Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by itself. Instead its requests other program like entities called functions in C to get its tasks done. A function is a self-contained block of statements that perform a coherent task of same kind The name of the function is unique in a C Program and is Global. It neams that a function can be accessed from any location within a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing. We can divide a long C program into small blocks which can perform a certain task. A function is a self-contained block of statements that perform a coherent task of same kind.

JHUNJHUNWALA BUSINESS SCHOOL

PRAMOD YADAV

Structure of a Function
There are two main parts of the function. The function header and the function body. int sum(int x, int y) { int ans = 0; ans = x + y; return ans } //holds the answer that will be returned //calculate the sum //return the answer

Function Header In the first line of the above code int sum(int x, int y)

It has three main parts 1. The name of the function i.e. sum 2. The parameters of the function enclosed in paranthesis 3. Return value type i.e. int

Function Body Whatever is written with in { } in the program is the body of the function.

JHUNJHUNWALA BUSINESS SCHOOL

PRAMOD YADAV

Function Prototypes The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. The prototype of the function in the above example would be like int sum (int x, int y); The only difference between the header and the prototype is the semicolon ; there must the a semicolon at the end of the prototype.

Functions in C/C++ Programming Functions groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by it self. Instead its requests other program like entities called functions in C to get its tasks done. A function is a self-contained block of statements that perform a coherent task of same kind.

JHUNJHUNWALA BUSINESS SCHOOL

PRAMOD YADAV

Why we use Functions


The most important reason to use functions is to aid in the conceptual organization of a program. Another reason to use functions is to reduce program size. Any sequence of instructions that appears in program more than once is a candidate for being made into a function. The functions code is stored in only one place in memory, even though the function is executed many times in the course of the program. Two reasons

1. Writing functions avoids rewriting the same code over and over. Suppose that there is a section of code in a program that calculates area of a triangle. If, later in the program we want to calculate the area of a different triangle we wont like to write the same instructions all over again. Instead we would prefer to jump to a section of code that calculates area and then jump back to the place from where you left off. This section of code is nothing but a function.

2. Using functions it becomes easier to write programs and keep track of what they are doing. If the operation of a program can be divided in to separate activities, and each activity placed in a different function, then each could be written and checked more or less independently. Separating the code in to modular functions also makes the pro-gram easier to design and understand.

JHUNJHUNWALA BUSINESS SCHOOL

PRAMOD YADAV

Function Declaration Just as we cant use a variable without first telling the compiler what it is, we also cant use a functions without telling the compiler about it, There are two ways to do this. The approach we show here is to declare the function before it is called. The other approach is to define it before it is called. We will examine functions starline() is declared in the line. Void starline ( ); The declaration tells the compiler that at some later point we plan to present a function called starline. The keyword void specifies that the function has no return value, and the empty parentheses indicate that it takes no arguments. Notice that the functions declarations is terminated with a semicolon It is a complete statement in itself. Function declarations are also called prototypes, since they provide a model or blueprint for the function. They tell the compiler, a function that looks like this is coming up later in the program, so its all right if you see references to it before you see the function itself.

Calling the Function


The function is called (or invoked) three times from main (). Each of the three calls look like this : Starline(): This is all we need to call a function name, followed by parentheses. The syntax of the call is very similar to that of declaration, except that the return type is not used. A semicolon terminates the call. Executing the call statement causes the function to execute; that is, control is transferred to the function, the statement in the function definition are executed, and then control returns to the statement following the function call.

JHUNJHUNWALA BUSINESS SCHOOL

PRAMOD YADAV

Function Definition Finally, we come to the functions its self, which is referred to as the functions definition, The definition contains the actual code for the function. Heres the definition for starline void starline ( ) { for ( intj=0, j < 45; j++ ) cout << "*" ; cout << endl; } The definition consists of a line called the decelerator, followed by the function body , The function body is composed of the statements that make up the function, delimited by braces. The decelerator must agree with the declaration: It must use the same function name, have the same argument types in the same order( if there are arguments), and have the same return type. Notice that a semicolon does not terminate the declarator. Figure shows the syntax of the function declaration, function call, and function definition. When the function is called, control is transferred to the first statement in the functions body. The other statements in the function body are then executed, and when the closing brace is encountered, control returns to the calling program. There are basically two types of functions 1. Library functions Ex. printf ( ), scanf ( ) etc. 2. User defined function

JHUNJHUNWALA BUSINESS SCHOOL

PRAMOD YADAV

Function is defined when function name is followed by a pair of braces in which one or more statements may be present for e.g. message ( ) { statement 1; statement2; statement 3; } PASSING ARGUMENTS TO FUNCTION An argument is a piece of data (an into value, for exp) passed from a program to the function. Arguments allow a function to operate with different values, or even to do different things, depending on the requirements of the program calling it. RETURNING VALUES FROM FUNCTIONS When a function completes its execution, it can return a single value to the calling program. Usually this return value consists of an answer to the problem the function has solved. When a function returns a value. The data type of this value must be specified. The function declaration does this by placing the data types, float in this case, before the function name in the declaration and the definition. Functions in earlier program examples returned no value, so the return type was void. In the above function lbstokg() returns type float, so the declaration is Float lbstokg(float); The first float specifies the return type. The float in parentheses s specifies that an argument to be passed to lbstokg() is also of type float.

JHUNJHUNWALA BUSINESS SCHOOL

PRAMOD YADAV

CALL BY VALUE In the preceding examples we have seen that whenever we called a function we have always passed the values of variables to the called function. Such function calls are called calls by value by this what it meant is that on calling a function we are passing values of variables to it. The example of call by value are shown below ; sum = calsum (a, b, c); f = factr (a); In this method the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. With this method the changes made to the formal arguments in the called function have no effect on the values of actual argument in the calling function.

CALL BY REFERENCE In the second method the addresses of actual arguments in the calling function are copied in to formal arguments of the called function. This means that using these addresses we would have an access to the actual arguments and hence we would be able to manipulate them the following program illustrates this.

JHUNJHUNWALA BUSINESS SCHOOL

PRAMOD YADAV

Das könnte Ihnen auch gefallen