Lecture 23: Functions Of C/C++ in Punjabi Language Part-1
Computer Gyan
Why we use functions? We use functions to simplify our work. Functions are mostly used in c for a series of instructions that are to be executed more than once. But it is not like a loop. A loop can repeat a series of instructions only if each statement follows the previous one. By calling a function a series of instructions can be repeated at any point within the program. Thus by the use of functions the program becomes short which is easy to write and debug.
Function name Function return type (if there is no return then use void) e.g. function return-type (void), Function argument (if there is no argument here we can also use void) Syntax to define a function: Return type functionname (arguments) We can divide the user define function in following main categories
- No return no argument
- No return with argument
- Return without argument
- Return with argument
A function is a self-contained program segment that carries out some specific ,well-defined task. A function is also known as sub program that can execute by it in the program. Function basically are classified in two categories Library function :- These are needed in every program they are stored in the library files known as header file. Commonly useable functions are printf() and scanf().it is to included at the beginning of the program using pre –processors. User-defined function :- These functions are defined by the users according to their requirements. A recursive function is one which calls itself. This is another complicated idea which you are unlikely to meet frequently. We shall provide some examples to illustrate recursive functions. Recursive functions are useful in evaluating certain types of mathematical function. You may also encounter certain dynamic data structures such as linked lists or binary trees. Recursion is a very useful way of creating and accessing these structures. The major advantages of using functions are as follows: Efficiency of maintenance of code Elimination of redundancy of code Ease of understanding Reusability of code
Function call means calling a function for the purpose of execution. A function can be called by simply using the function name in a statement. Eg. Main() { int p; p=mul(10,5); printf (“%d\n”,p); } When the compiler encounters a function call, the control is transferred to the function mul (x,y). This function is then executed line by line as described and a value is returned when a return statement is encountered. This value is assigned to p. The process of passing values to functions is known as parameter passing, with the help of parameters different types of values and number of values can be passed to functions. Parameter passing is of two types.
Call by value: In call by value the original values will be copi ... https://www.youtube.com/watch?v=2hzavyGNBmQ
73079132 Bytes