Function

485 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
485
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
21
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Function

  1. 1. FUNCTIONS’ IN C By-Nilam Desai
  2. 2. WHAT IS FUNCTION? A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a  C Program as required. It also optionally returns a value to the calling program
  3. 3. FUNCTION PROPERTIES Every function has a unique name. This name is used to call function from “main()” function. A function can be called from within another function. A function is independent and it can perform its task without intervention from or interfering with other parts of the program. A function performs a specific task. A task is a distinct job that your program must perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc.
  4. 4. PROPERTIES CONT… A function returns a value to the calling program. This is optional and depends upon the task your function is going to accomplish. Suppose you want to just show few lines through function then it is not necessary to return a value. But if you are calculating area of rectangle and wanted to use result somewhere in program then you have to send back (return) value to the calling function.
  5. 5. TYPES OF C FUNCTION Inbuilt function: they are already defined in C library.  i.e. Printf, scanf, clrscr etc.  User define function: programmer will create it based on program requirements.  i.e. main
  6. 6. GENERAL STRUCTURE OF A CFUNCTION <return type> FunctionName (Argument1, Argument2, Argument3……) { Statement1; Statement2; Statement3; } Example  void main() { }  int sum (int x, int y) { int result; result = x + y; return (result); }
  7. 7. FUNCTION PROTOTYPE ANDFUNCTION DEFINITION Function prototype tells compiler that we are going to use a function which will have given name, return type and parameters. Function definition on the other hand is just writing logic of any function.  For example you have one function prototype in C program for adding two integer numbers  int add(int, int)  but along with prototype you also have to write logic how that function will behave (respect to above prototype; how you will utilize those two passed integer value and how you will return value) when you will call that function.
  8. 8. FUNCTION PROTOTYPE ANDFUNCTION DEFINITION EXAMPLE #include<stdio.h> #include<conio.h> void myFunction(); int add(int, int);   void main() {     clrscr();       myFunction();     printf("nn%d",add(10,15)); getch(); }
  9. 9. EXAMPLE CONT… void myFunction(){     printf("This is inside function :D");}  int add(int a, int b){     return a+b;}
  10. 10. EXPLANATION We will start from line no. 4 & 5. In these two lines we have function prototype, line no. 4 has a simple function prototype which doesn’t have parameters or return type. It’s very simple to declare a prototype, see line no. 17 & 4, both line are same except prototype requires semicolon (;) at the end. Code block (line no. 17-20) is function definition for our first function prototype (i.e. line no. 4). In this block we are defining behavior of that function and in my code it’s just printing a simple message for demonstration.
  11. 11.  Similarly we have second function prototype (line no. 5) and it has parameters and return type. Code block (line no. 22 – 25) is definition of above function prototype. One important point for function prototype having parameters; in prototype, variable name for parameters is optional (see line no. 5). There is no variable name for int parameters, its (int, int). But in that prototype’s function definition you must provide parameter variable name (see line no. 22). We have (int a, int b) parameter name. One more important thing, if you writing function definition above main() function then you don’t need to write prototype of that function. But it’s a good practice to keep all the functions below main() function.
  12. 12. ADVANTAGES OF USINGFUNCTIONS: It makes possible top down modular programming. In this style of programming, the high level logic of the overall problem is solved first while the details of each lower level functions is addressed later. The length of the source program can be reduced by using functions at appropriate places. It becomes uncomplicated to locate and separate a faulty function for further study.
  13. 13. ADVANTAGES CONT… A function may be used later by many other programs this means that a c programmer can use function written by others, instead of starting over from scratch. A function can be used to keep away from rewriting the same block of codes which we are going use two or more locations in a program. This is especially useful if the code involved is long or complicated.
  14. 14. TYPES OF USER DEFINEDFUNCTIONS: Functions with no arguments and no return values. Functions with arguments and no return values. Functions with arguments and return values. Functions that return multiple values. Functions with no arguments and return values.
  15. 15. OUTPUT
  16. 16. EXPLANATION This program has only function named “add()” . Calling this C function from “main()” is very simple. This “add()” function takes two values as arguments, adds those two values and prints the result. Line 3-8 is a function block of the program. Line no. 3 is the header of function,void is return type of function, add is function name and (int x, int y) are variable which can hold integer values to x and y respectively.
  17. 17.  When we call function, line no. “12, 13, 14”, we need to send two integer values as its argument. Then these two values get stored in variable x and y of line no. 3. Now we have two values to perform addition; in line no. 5 there is an integer declaration named “result”. This integer will store the sum of x and y (please see line no. 6). Line no. 7 simply prints the result with C’s inbuilt function “printf”. Now imagine the same program without using function. We have called “add()” function three times, to get the same output without using function we have to writeLine no. 6 & 7 three time. If you want to add more value later in the program then again you have to type those two lines.
  18. 18. NESTING OF FUNCTION Function calls within another function is called Nesting of function. C allows nesting of functions easily. Main() can call function1,function1 can call function2 and so on.. Main() Function1() Function2()
  19. 19. RULES FOR PASSING ARRAY ASFUNCTION PARAMETER ORARGUMENT The function prototype must show that the argument is an array. In function definition the formal argument must be an array if the argument list have an array as parameter. Function call must have only the name of an array .indexing of the array is not required to pass an whole array.

×