Lecture20 user definedfunctions.ppt

25
Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar USER DEFINED FUNCTIONS IN ‘C’ Prakash Khaire

description

user defined functions

Transcript of Lecture20 user definedfunctions.ppt

Page 1: Lecture20 user definedfunctions.ppt

Lecturer, B V Patel Inst. of BMC & IT, Gopal Vidyanagar

USER DEFINED FUNCTIONS IN ‘C’

Prakash Khaire

Page 2: Lecture20 user definedfunctions.ppt

Introduction

●Strength of ‘C’ language is C functions●They are easy to define and use●We are very much familiar with main(), printf() & scanf() function

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarB V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 3: Lecture20 user definedfunctions.ppt

In this chapter, you will see…

●How a function is designed ?●How a function is integrated into a program ?●How two or more functions are put together /●How they communicate with one another ?

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 4: Lecture20 user definedfunctions.ppt

Definition - function●A set of statements working together with common goal is known as function.●Also known as subprograms which are used to compute a value or perform a specific task.●They can’t run independently and are always called by the main() program or by some other function.Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 5: Lecture20 user definedfunctions.ppt

Categories of fucntions●In ‘C’ language, functions are classified into the following two categories●Library functions●User-Defined functions●scanf(), printf(), getch(), strlen(), strcmp(), strcat(), sqrt(), pow() are this are library functions.●main() is user-defined functionsPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 6: Lecture20 user definedfunctions.ppt

User Defined Functions●User defined functions are self-contained blocks of statements which are written by the user to compute or perform a task.●They can be called by the main program repeatedly as per the requirement.

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 7: Lecture20 user definedfunctions.ppt

Uses of functions●They are very much useful when a block of statements has to be written/executed again and again.●They are useful when program size are too large and complex. ●It works like top-down modular programming technique to solve a problem.●They are also used to reduce the difficulties during debugging a program.Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 8: Lecture20 user definedfunctions.ppt

Uses of functions●The length of a source program can be reduced by using functions at appropriate places.●It is easy to locate and isolate a faulty function for further investigations.●A function can be used by many other programs. Thus C programmer can build their own library. Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 9: Lecture20 user definedfunctions.ppt

Top-down modular programming using functions

Main program

Function A Function B Function C

B1

B1

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 10: Lecture20 user definedfunctions.ppt

A MULTI-FUNCTION PROGRAMvoid main(){printline();printf(“I love my parents !!!”);printline();}void printline(){

int I;for(i=0;i<40;i++)printf(“-”);

printf(“\n”);}

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 11: Lecture20 user definedfunctions.ppt

ELEMENTS OF USER DEFINED FUNCTION

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

●Some similarities between functions and variables●Both functions names and variables are considered as identifiers and therefore they must follow the rules for identifiers●Like variables, functions have type associated with them●Like variables, function names and their types must be declared and defined before they are used in programPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 12: Lecture20 user definedfunctions.ppt

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

●In order to make use of user-defined functions, we need to establish three elements that are related to functions.●Function definition●Function Call●Function declaration

ELEMENTS OF USER-DEFINED FUNCTION

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 13: Lecture20 user definedfunctions.ppt

FUNCTION DEFINITION

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

●The function definition is an independent program module that is specially written or implement the requirements of the function.●To use this block or function, we need to invoke it at the required place in the program, known as the functions● The program that calls the function is referred to as the calling program or calling functions Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 14: Lecture20 user definedfunctions.ppt

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

main(){ ….. function1(); …. …. function2();}function1(){ … …}function2(){ … function1();}

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 15: Lecture20 user definedfunctions.ppt

FLOW OF FUNCTION

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

●When the program is executed (that is, run) execution always begins at the first statement in the function main no matter where it is placed in the program.

●Other functions are executed only when they are called. ●Function prototypes appear before any function definition, so the

compiler translates these first. The compiler can then correctly translate a function call.

●A function call statement results in the transfer of control to the first statement in the body of the called function.

●After the last statement of the called function is executed, the control is passed back to the point immediately following the function call.

●A value-returning function returns a value. Therefore, for value-returning functions, after executing the function when the control goes back to the caller, the value that the function returns replaces the function call statement.

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 16: Lecture20 user definedfunctions.ppt

FUNCTION DEFINITION●A function definition includes the following elements●Function name●Function type●List of parameters●Local variable declarations●A return statement

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 17: Lecture20 user definedfunctions.ppt

A general format of function definition

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

datatype functionName(parameter list){

local variable declarations; executable statement1; executable statement2; … … return statement }

Function header 1. function type 2. function name 3. formal parameter list

function body

returns the value - optional

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 18: Lecture20 user definedfunctions.ppt

●If no return data type is specified than by default ‘C’ will assume that it is an integer type.●If the function is not going to return any value then we have to specify the return type as void.●Function name follows the rules of identifier. Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

A general format of function definition

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 19: Lecture20 user definedfunctions.ppt

●Parameter list are list of variables that will receive the data sent by the calling program. ●int sum(int a, int b) { ………………… }●float mul(float x, float y) { ………………. }

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

A general format of function definition

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 20: Lecture20 user definedfunctions.ppt

Calling a function●A function is called by the calling program using the function name with the required number of arguments in parenthesis.●The function call comes in assignment statement or in an output statement.●printf(“%d”,sum(a,b));●ans = sum(a,b);●A function is called using its name with required number of arguments in paranthesis. Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 21: Lecture20 user definedfunctions.ppt

Formal and Actual Argumentsint sum(int, int); //declarationvoid main(){

int a=5, b=6,ans; ans =sum(a , b);//calling function printf(“Answer : %d”,ans);}

//function definitionint sum(int x, int y) {

int val; val = x +y; return val;}

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

actual arguments

formal arguments

The argument listed in the function calling statement are referred to as actual arguments

The arguments used in the function declaration are referred as formal arguments

Prakash Khaire, Lecturer

B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 22: Lecture20 user definedfunctions.ppt

Formal and Actual Arguments●The argument listed in the function calling statement are referred to as actual arguments.●They are the actual values passed to a function to compute a value or to perform a task.

●The agrument used in the function declaration are referred as formal arguments.●They are simply formal variables that accepts or receive the values supplied by the calling program.

●Prakash Khaire, Lecturer

B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer

B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 23: Lecture20 user definedfunctions.ppt

Rules of call a function●Function is called by the main() or any other function●When the return type of the function is omitted, then by default the return type will be int.●A function can return a value any stage of its execution by using more than one return statements.●The return statement is omitted if it does not return a value directly to the calling program.●Arguments listed can be omitted, but not the paranthesis () following the function.

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 24: Lecture20 user definedfunctions.ppt

Function Declaration or prototype●To match the number and data type of the actual and formal arguments in the calling and called function respectively.●To check this compiler will verify the function declaration or prototype.●data_type function_name(data_type var1, data_type var2,…..,data_type varn);●Example●int sum(int, int);●The prototype declaration is always written above the main() function definition.

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar

Page 25: Lecture20 user definedfunctions.ppt

Return values and their types●We can pass n numbers of values to the called function, but the called function can only return one value per call.●The return statement can take one of the following form●return;●return(expression);●The plain return does not return any value ●return statement with expression returns the value of the expression●There can be more than one return statement if there is use of conditional statement.

Prakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal VidyanagarPrakash Khaire, Lecturer B V Patel Inst. of BMC & IT, Gopal Vidyanagar