morteza noferesti
play

Morteza Noferesti Experience has shown that the best way to develop - PowerPoint PPT Presentation

Morteza Noferesti Experience has shown that the best way to develop and maintain a large program is to construct it from smaller pieces or modules, each of which is more manageable than the original program. This technique is called


  1. Morteza Noferesti

  2.  Experience has shown that the best way to develop and maintain a large program is to construct it from smaller pieces or modules, each of which is more manageable than the original program.  This technique is called divide and conquer.  Modules in C are called functions.

  3.  A function is a group of statements that together perform a task. Every C program has at least one function, which is main() , and all the most trivial programs can define additional functions.  You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.

  4.  A boss (the calling function or caller) asks a worker (the called function) to perform a task and report back when the task is done

  5.  For example, a function needing to display information on the screen calls the worker function printf to perform that task, then printf displays the information and reports back — or returns — to the calling function when its task is completed.  The boss function does not know how the worker function performs its designated tasks  The worker may call other worker functions, and the boss will be unaware of this.

  6.  Standard library functions ◦ inbuilt functions in C programming. ◦ The prototype and data definitions of the functions are present in their respective header files, and must be included in your program to access them. ◦ For example: If you want to use printf() function, the header file <stdio.h> should be included.  User defined functions ◦ C allow programmers to define functions. Such functions created by the user are called user-defined functions. ◦ Depending upon the complexity and requirement of the program, you can create as many user-defined functions as you want. ◦ These are sometimes referred to as programmer-defined functions.

  7.  Maintenance and debugging: ◦ The program will be easier to understand, maintain and debug.  Reusability: ◦ Reusable codes that can be used in other programs  Encapsulation: ◦ A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.

  8.  Functions are normally used in a program by writing the name of the function followed by a left parenthesis followed by the argument (or a comma-separated list of arguments) of the function followed by a right parenthesis.  For example, a programmer desiring to calculate and print the square root of 900.0 might write printf( "%.2f", sqrt( 900.0 ) );  When this statement executes, the math library function sqrt is called to calculate the square root of the number contained in the parentheses (900.0).

  9.  The general form of a function definition in C programming language is as follows ReturnType functionName (type1 argument1,type2 argument2,...); { body of the function }

  10.  Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void .  Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.  Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.  Function Body − The function body contains a collection of statements that define what the function does.

  11. Functi tion on Name Parameter ters Return Type int addNumbers (int a,int b) { int c; c=a+b; return c; } Functi tion on Body

  12.  A function Prototype tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. returnType functionName (type1 argument1, type2 argument2,...);

  13. int addNumbers (int a,int b) { int c; c=a+b; return c; } int addNumbers(int a, int b);  is the function prototype which provides following information to the compiler: ◦ name : addNumbers() ◦ return type : int ◦ arguments type : two integers are passed to the function

  14. int max(int num1, int num2) ; /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }

  15. #include <stdio.h> #include <stdio.h> Function prototype; Function definition int main() int main() { { ... ... ... ... functionName(); functionName(); ... ... ... ... } } Function definition

  16.  All variables defined in function definitions are local variables —they’re known only in the function in which they’re defined.  Most functions have a list of parameters that provide the means for communicating information between functions.

  17. Function to compute square of a number

  18.  Parameter names are not important in function declaration only their type is required, so the following is also a valid declaration  Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.

  19.  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 called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.  To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.

  20. #include <stdio.h> /* function declaration */ 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; } /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }

  21.  In programming, argument refers to the variable passed to the function.  The parameters a and b accepts the passed arguments in the function definition. These arguments are called formal parameters of the function.  The type of arguments passed to a function and the formal parameters must match, otherwise the compiler throws error.  If n1 is of char type, a also should be of char type. If n2 is of float type, variable b also should be of float type.  A function can also be called without passing an argument.

  22.  The return statement terminates the execution of a function and returns a value to the calling function. The program control is transferred to the calling function after return statement.  In the above example, the value of variable result is returned to the variable sum in the main() function.

  23. return (expression);  For example, return a; return (a+b);  The type of value returned from the function and the return type specified in function prototype and function definition must match.

  24.  To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack.  Students can think of a stack as analogous to a pile of dishes.  When a dish is placed on the pile, it’s normally placed at the top (referred to as pushing the dish onto the stack).  Similarly, when a dish is removed from the pile, it’s always removed from the top (referred to as popping the dish off the stack).  Stacks are known as last-in, first-out (LIFO) data structures — the last item pushed (inserted) on the stack is the first item popped (removed) from the stack.

  25.  When a program calls a function, the called function must know how to return to its caller, so the return address of the calling function is pushed onto the program execution stack (sometimes referred to as the function call stack).  If a series of function calls occurs, the successive return addresses are pushed onto the stack in last-in, first-out order so that each function can return to its caller.  The program execution stack also contains the memory for the local variables used in each invocation of a function during a program’s execution.

  26.  This data, stored as a portion of the program execution stack, is known as the activation record or stack frame of the function call.  When a function call is made, the activation record for that function call is pushed onto the program execution stack.  When the function returns to its caller, the activation record for this function call is popped off the stack and those local variables are no longer known to the program.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend