Unit 4 CP

of 19
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Related Documents
  Functions Function is a self contained block or sub-program of one or more statements that  performs particular task when called. A function has a name, a list of arguments which it takes when called, and the block of code it executes when called. Functions are two types: o   Built-in / Library function. ex:) printf(), scanf(), getch(), exit(), etc. o   User defined function. User-Defined Functions Functions defined by the users according to their requirements are called user-defined functions. These functions are used to break down a large program into a small functions. Library function Library functions are the in-built function in C programming system.  Advantage of User defined function: 1.   The length of a source program can be reduced by using functions at a appropriate  places. 2.   It is easy to locate and isolate a faulty function for further investigations 3.   A function may be used by many other programs. This means that a C programmer can  build on what others have already done, instead of starting from scratch. Body of user defined function: return_type f_name (argument1,argument 2) { local variables; statements; return_type; }    As mentioned earlier, every C program begins from main() and program starts executing the codes inside main() function.    When the control of program reaches to function_name() inside main() function.    The control of program jumps to void function_name() and executes the codes inside it.    When, all the codes inside that user-defined function are executed, control of the program  jumps to the statement just after function_name() from where it is called.   Remember, the function name is an identifier and should be unique. Write a C program to add two integers. Make a function  add to add integers and display sum in  main() function.  /*Program to demonstrate the working of user defined function*/ #include <stdio.h> int add(int a, int b); //function prototype(declaration) int main(){ int num1,num2,sum;  printf( Enters two number to add\n ); scanf( %d %d ,&num1,&num2); sum=add(num1,num2); //function call  printf( sum=%d ,sum); return 0; } int add(int a,int b) //function declarator { /* Start of function definition. */ int add; add=a+b; return add; //return statement of function /* End of function definition. */ } C function aspects syntax 1 function definition return_type function_name ( arguments list ) { Body of function; } 2 function call function_name ( arguments list ); 3 function declaration return_type function_name ( argument list );  FUNCTION DECLARATION Like variables a function must be declared before it can be used (called).The declaration of a function resembles the definition, return-type function-name(  parameter types ); The function body is replaced by a semi-colon. Parameters need not be named, it is sufficient to specify their types. Example, declaring average double average(double, double); Declaration is not necessary if the function definition precedes the first call. Function Definitions return-type function-name(  parameters ) { declarations  statements return value ;  } return-type type of value returned by function or void if none function-name unique name identifying function  parameters comma-separated list of types and names of parameters value value returned upon termination(not needed if return-type void) The list of parameters is a declaration on the form type 1 par 1, ..., type n par n and represents external values needed by the function. The list of parameters can be empty.All declarations and statements that are valid in the main program can be used in the function definition, where they make up the  function body . Example, computing averages double average(double x, double y) { return (x+y)/2;  } The return statement forces the function to return immediately, possibly before reaching the end of the function body. Function calls variable =function-name( arguments ); variable is assigned the return-value of the function. The arguments are values with types corresponding to the function parameters.For example, int main(int argc, char **argv) { double a=1; double avg; avg = average(a, 3.0); return 0; } Implicit type conversion is used when the types of the arguments and parameters do not agree. For example consider the following segment of program main() { float x,a,b, mul(); //function declaration double y, div(); //function declaration //Program statements x = mul(a,b); //function call y = div(a,b); //function call } float mul(t,e) //function definition float t,e; { // Body } double div(i,o) //function definition float i,o; { //body }


Jul 23, 2017
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks

We need your sign to support Project to invent "SMART AND CONTROLLABLE REFLECTIVE BALLOONS" to cover the Sun and Save Our Earth.

More details...

Sign Now!

We are very appreciated for your Prompt Action!