lecture-07 function 08.pdf · 1.function definition: is an independent module that is specially...

29
LECTURE - 07 FUNCTION PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET. 1

Upload: others

Post on 18-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

LECTURE-07FUNCTION

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

1

Page 2: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

2

FUNCTIONA function is a group of statements thattogether perform a task. Every C program has atleast one function, which is main(), and all themost trivial programs can define additionalfunctions. C functions can be classified into twocategories viz. library functions and userdefined functions.You can divide up your code into separatefunctions.

Page 3: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

3

How you divide up your code amongdifferent functions is up to you, but logicallythe division is such that each functionperforms a specific task. A functiondeclaration tells the compiler about afunction's name, return type, andparameters. A function definition providesthe actual body of the function. A functioncan also be referred as a method or a sub-routine or a procedure, etc.

Page 4: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

4

Two types of function:Library function:Pre-defined functions which are not requiredto be written by user. Example: printf(),scanf().

User defined function:A function that is declared by user is calleduser-defined function. This has to bedeveloped by user at the time of writing of aprogram.

Page 5: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

5

Elements of function:1.Function definition: is an independent

module that is specially written toimplement the requirements function.

2.Function call: To use a function we need toinvoke it at required place in the program.

3.Function declaration: The calling programshould declare any function that is to beused in the program.

Page 6: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

6

Defining a FunctionThe general form of a function definition in Cprogramming language is as follows:return_type function_name( parameter list ){body of the function}A function definition in C programmingconsists of a function header and a functionbody.

Page 7: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

7

Here are all the parts of a function:Return Type:A function may return a value. The return_typeis the data type of the value the functionreturns. Some functions perform the desiredoperations without returning a value. In thiscase, the return type is the keyword void.Function Name:This is the actual name of the function. Thefunction name and the parameter list togetherconstitute the function signature.

Page 8: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

8

Parameters:A parameter is like a place holder. When afunction is invoked, you pass a value to theparameter. This value is referred to as actualparameter or argument. The parameter listrefers to the type, order, and number of theparameters of a function. Parameters areoptional; that is, a function may contain noparameters.

Page 9: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

9

Function Body: The function body contains acollection of statements that define what thefunction does.• Function name• Function type• List of parameters• Local variable declaration• Function statement• Return statement

Function header

Function body

Page 10: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

10

ExampleGiven below is the source code for a function called max().This function takes two parameters num1 and num2 andreturns the maximum value between the two:/* function returning the max between two numbers */int max(int num1, int num2){/* local variable declaration */int result;if (num1 > num2)result = num1;elseresult = num2;return result;}

Page 11: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

11

Function DeclarationsA function declaration tells the compilerabout a function name and how to call thefunction. The actual body of the function canbe defined separately. A function declarationhas the following parts:return_type function_name( parameter list );

Page 12: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

12

For the above defined function max(),thefunction declaration is as follows:

int max(int num1, int num2);Parameter names are not important in functiondeclaration, only their type isrequired, so the following is also a validdeclaration:

int max(int, int);

Page 13: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

13

Function declaration is required when youdefine a function in one source file and youcall that function in another file. In such case,you should declare the function at the top ofthe file calling the function.A function declaration also known asfunction prototype.

Page 14: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

14

Calling a Function• While creating a C function, you give a

definition of what the function has to do.• To use a function, you will have to call that

function to perform the defined task.• When a program calls a function, the

program control is transferred to the calledfunction.

Page 15: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

15

• A called function performs a defined taskand when its return statement is executedor when its function-ending closing braceis reached, it returns the program controlback to the main program.

• To call a function, you simply need to passthe required parameters along with thefunction name, and if the function returnsa value, then you can store the returnedvalue.

Page 16: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

16

For example:int max(int num1, int num2);int main (){/* local variable definition */int a = 100;int b = 200;int ret;/* calling a function to get max value */ret = max(a, b);printf( "Max value is : %d\n", ret );return 0;}We have kept max()along with main() and compiled the source code.

Page 17: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

17

Function Arguments• If a function is to use arguments, it must

declare variables that accept the values ofthe arguments. These variables are calledthe formal parameters of the function.

• Formal parameters behave like other localvariables inside the function and are createdupon entry into the function and destroyedupon exit.

Page 18: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

18

While calling a function, there are two ways inwhich arguments can be passed to a function:

Page 19: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

19

Category of Function:1.Category 1: Functions with no argument no

return value:-When a function has no arguments it does notreceive any data from the calling function.Similarly, when it does not return a value, thecalling function does not receive any data fromthe called function. In effect, there is no datatransfer between the calling function and thecalled function. It can only be used as anindependent statement.

Page 20: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

20

2. Category 2: Functions with argument noreturn value:-The main function has no control over the waythe functions receive input data. Example: Thefunction print line will print the same line eachtime, it is called. We could make the callingfunction to read data from the terminal andpass it on to the called function. The nature ofdata communication between the callingfunction and the called function with argumentsbut no return value.

Page 21: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

21

3. Category 3: Functions with argument onereturn value:-The function value receives data from thecalling function through arguments but doesnot send back any value. Rather, it displaysthe results of calculation at the terminals.However, we may not always wish to havethe results of a function displayed. We mayuse it in the calling function for furtherprocessing. Such functions will have two-waydata communication.

Page 22: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

22

4. Category 4: Functions with no argument onereturn value:-We may need to design functions that may nottake any arguments but returns a value to thecalling function. Example: getchar functiondeclared in the header file <studio.h>. Thegetchar function has no parameters but itreturns an integer type data that represents acharacter.

Page 23: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

23

Example:int get number (void);main(){ int m=get number();printf( “%d”, m); }

int get number (void);{Int number;scanf( “%d”, &number);return (number);}

Page 24: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

24

5. Category 5: Functions that return multiplevalues:• A return statement that can return only one

value. Suppose, however that we want to getmore information from a function. Thearguments that are used to send out informationare called output parameters.

• To get multiple return values from a functionoutput parameters are used as arguments.

• The mechanism of sending back informationthrough arguments is achieved by using theaddress operator (&) and indicator operator (*).

Page 25: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

25

Example:void math p(intx, inty, int*s, int*d);main(){ intx=20, y=10, s,d;

math p(x, y, &s, &d);printf(“s=%d\nd=%d\n”,s, d);

}void math p(intx, inty, int*s, int*d);{*s=a+b;*d=a-b;}

Page 26: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

26

Nesting of functions:C permits nesting of functions freely. Main can callfunction 1, which calls function 2…………which callsfunction 3 ……..and so on.Example:float ratio (intx, inty, intz)int difference (intx, inty)main(){int a,b,c;scanf(“%d%d%d”, &a, &b, &c);printf(“%f\n”, ratio (a, b, c));}

Page 27: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

27

float ratio (intx, inty, intz){ if (difference (y,z))return (x/(y-z));elsereturn(0.0);}Int difference (intp, intq){ if (p!=q)return(1);

elsereturn (0);}

Page 28: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

28

Recursion:When a called function in turn calls anotherfunction a process of chaining occurs. Recursionis a special case of this process, where afunction calls itself.main(){printf(“This is an example”);main()…………………………..………………………… }

Page 29: LECTURE-07 FUNCTION 08.pdf · 1.Function definition: is an independent module that is specially written to implement the requirements function. 2.Function call: To use a function

PINGKI DATTA, LECTURER, DEPARTMENT OF CIVIL ENGINEERING, KUET.

29

Thanks to Everyone

See you in next class.