Professional Documents
Culture Documents
Version 12.0
term argument refers to the value that you supply corresponding to a parameter when you call a function. Parameters provide the means by which you pass information from the calling function into the function that is called. A variable or an expression or a constant can be used as an argument.
Points to remember
Functions cannot return arrays or functions although they can return pointers. There is no restriction on the possible number or type of parameters that may be associated with a function. The number of parameters a function has also directly affects the speed at which it is called the more parameters, the slower the function call. Therefore, if possible, you should minimize the number of parameters you use in a function. . One technique that can be helpful if you find yourself with a large number of function parameters is to put your function parameters in a structure. Function definitions may not be included within other function definitions, i.e. they cannot be nested. Function call is a prefix expression. Therefore when a function is used as a part of large expression, it will be evaluated first unless parentheses are used to specify a different evaluation order. Functions are identified by name and can return a single value.
Version 12.0
If a variable of the same name is declared both within a function and outside of it, the function will use the variable that is declared within it and ignore the global one. If not initialized, a global variable is initialized to zero by default.
where use of global variables is advocated. Global variables, however, are extremely useful in creating array of data and constants that must be shared between many functions. If many functions require access to a group of arrays, global variables allow the functions to make efficient changes to the same array without the need for multiple arrays passing.
SCOPE
The region of the program over which an identifier is visible is called the scope of the identifier. The scope relates to the accessibility, the period of existence, and the boundary of usage of variables declared in a program.
The data object created with the use of special library functions such as malloc() or calloc() etc have dynamic duration and the storage remain allocated from the time of creation at run time until program termination or until a call to special library function free().
Types of Scope
Scopes can be of four types in C. o block o file o function o function prototype Block Scope This means that the identifier can only be used in the block in which it is declared. These variables are created at the point of their declaration inside the block and cease to exist outside it Functions are considered as named block. Variables declared within a function block can be used anywhere within the function in which they are defined.
Page 2
Version 12.0
Function Scope This applies only to labels. Normally labels are used with goto statement. It simply means that labels can be used anywhere within the function in which they are defined. This includes use before definition. File Scope This means that the identifier can be used anywhere in the current file after the declaration of the identifier. This applies to functions and all variables declared outside functions. File scope variable is also known as global variable. Function Prototype Scope In order to improve readability function prototypes are usually written with "dummy" variable names. For example double max(double x, double y); The identifiers "x" and " y" have function prototype scope, which terminates at the end of the prototype.
Identifiers with file scope declared as static have internal linkage. These are known only within the file in which they are declared. Local identifiers have no linkage and are therefore known only within their own block. This includes the function's formal parameters (which can be treated like ordinary variables within the function) as well as definitions appearing within the function.
Two declarations of the same identifier in a single file that have the same linkage, either internal or external, refer to the same object. The same identifier cannot appear in a file with both internal and external linkage. For the three cases above, you will want external linkage, internal linkage, and no linkage respectively. The external linkage declarations would be prefixed with extern, the internal linkage declarations with static.
#include <stdio.h> // External linkage. extern int var1; // Definitions with external linkage. extern int var2 = 0; // Internal linkage: static int var3; // Function with external linkage void f1(int a){} // Function can only be invoked by name fro m within this file. static int f2(int a1, int a2) { return(a1 * a2); }
LINKAGE
An identifiers linkage determines which of the references to that identifier refer to the same object. C defines three types of linkages external, internal and no linkage. A variable with external linkage can be used anywhere in a multi-file program. A variable with internal linkage can be used anywhere in a single file, and a variable with no linkage can be used only in the block where it is defined. External linkage means they are available from the point of definition throughout the entire remaining text of the program, even across subsequently-compiled files. Internal linkage means internal to the file containing the program code. The scope of such a name is from the textual point of definition to the end of the file. i.e. you can refer to it in any line following the one in which it is defined. Variables with internal or external linkage can appear in more than one declaration, but variables with no linkage can be declared only once. In general, Functions and global variables have external linkage. This means they are available to all files that constitute a program.
Points to note
It is not always necessary to specify both the storage class and the type of identifiers in a declaration. Storage class specifiers appearing in declarations outside of functions are assumed to be extern. In a declaration inside a function, if a type but no storage class is indicated, the identifier is assumed to be auto. An exception to the latter rule is made for functions because auto functions do not exist. It is implicitly declared to be extern.
Page 3
Version 12.0
Those three dots ... allows the function to accept any number of parameters. Only the last parameters must be NULL and the function prototype must be as follows void function_name(const char *, ) ; The macros used are va_list, va_start(), va_arg(), and va_end(). va_list is an array or special pointer' type which is used in obtaining the arguments that come in place of ellipsis. va_start() begins the processing of an argument list, va_arg() fetches argument values from it, and va_end() finishes processing. Therefore, va_list is a little bit like the stdio FILE * type, and va_start is a bit like fopen(). Consider the following program.
#include <stdarg.h> #include <stdio.h> void show(int n, ...) { va_list ap; va_start(ap, n); printf("count = %d: ", n); while (n-- > 0) { int i = va_arg(ap, int); printf("%d ", i); } printf("\n"); va_end(ap); } int main() { show(1, 1); show(3, 1, 2, 3); return 0; }
atexit() function.
The function whose address is passed to atexit() gets called before the termination of program. If atexit() is called for more than one function then the functions are called in "first in last out" order. You can verify that from the output.
#include <stdio.h> #include <stdlib.h> void fun1(void) { printf("Inside fun1\n"); } void fun2(void) { printf("Inside fun2\n"); } int main(void) { atexit ( fun1 ) ; atexit ( fun2 ) ; printf ("This is the last statement\ of program?\n" ); return 0; }
Page 4
Version 12.0
The show() function declares a single parameter (n), followed by an ellipsis (...). The ellipsis specifies that a variable number of additional parameters are present. This means that a caller of show() can pass an arbitrary number of arguments in addition to the first int argument. The output of this C program is: count = 1: 1 count = 3: 1 2 3 There are some restrictions on functions with variablelength arguments. The first parameter must be present, since its name has to be passed to va_start(). While calling the function, if the type of the variable passed does not match the type expected in the function, the results are unpredictable. The C/C++ variable argument mechanism is quite useful in certain contexts. But the mechanism is error-prone, because it defeats type checking. For example, if the second show() call is changed to: show(3, 1, 2, 3.4); The result is something like this: count = 1: 1 count = 3: 1 2 858993459 The program assumes that an int argument has been passed, when in fact a double (3.4) is passed. The macro va_end() performs cleanup operations. It releases any memory that might have been allocated when va_start() was called. Another function vprintf() used to develop a function that outputs an error and exits.
void error(const char *fmt) { va_list ap; va_start(ap, fmt); vprintf(fmt, ap); va_end(ap); exit(0); }
Notice that the function error() is called just like printf(). RECURSION
Recursion Vs Iteration
Iteration It is a process of executing a statement or a set of statements repeatedly, until some specified condition is specified. Iteration is more of a bottom up approach. It Recursion Recursion is the technique of defining anything in terms of itself.
Use of above function which prompts the user to enter an integer greater than 0, failing of which it outputs an error message and exits.
printf(\n enter an integer >0); scanf(%d, &n); if(n<=0) error(value of n= %d it must be\ greater than 0 \n, i);
Page 5
Recursion is generally a worse option to go for simple problems, or problems not recursive in nature.
FAQ
If a string str contains a string literal Oxford University Press, then is it legal to print the string using the statement printf(str);?
Yes. It prints Oxford University Press on the screen.
Output:
4 3 2 1 0 If the recursive call does not have base case as the following program then this will go on till a point where runtime error occurs due to stack overflow.
Page 6