User Defined Function
Function: A function is a named unit of a group of program statements. The unit can be invoked from other part of the program. The elements of function: 1. Function definition 2. Function call 3. Function declaration or Function prototype.
Function Prototype: A function prototype is a declaration of the function that tells the program about the type of the value returned by the function and the number and type of each argument. It consist of four parts. • Function type (return type) • Function name • Parameter list • Terminating Semicolon. They are coded in the following format. Function-type function-name (parameter list); Example: void sum(int a, int b); when we place the declaration above all the function(in the global declaration section ), the prototype is referred to as global prototype. Such declaration is available for all the function in the program. When we place it in a function definition ( in the local declaration section), the prototype is called a local prototype.
Function Definition:
A function must be defined before it is used anywhere in the program. The general form of a function definition is given below: type function-name(parameter list) { Body of the function } Where the type specifies the type of values that the return statement of the function returns. It may be any valid data type. If no type is specified, the compiler assumes the function returns as integer value. The parameter list is a comma-separated list of variables of a function referred to as its arguments. A function may be without any parameters, in which case, the parameter list is empty.
Function call: A function can be called by simple using the function name followed by a list of actual parameter (or arguments),if any, enclosed in parentheses. The general form: function-name(list of variable or value); eg. add(4,5);
When the compiler encounters the function call, the control is transferred to the function definition. And it executes each line of code in function and returns the control to the main program again.
Advantage of function:
1. It facilitates top-down modular programming.
2. The length of source program can be reduced by using functions at appropriate places.
3. It is easy to locate and isolate a faulty function for further investigations. 4. A function may be used by many other program.
The functions are categories as follows:
1. Function with no arguments and no return values: When a function has no argument, it does not receive any data from the calling function. Similarly, when it does not return value , the calling function does not receive any data from the called function. In effect, there is no data transfer between the calling function and the called function.
2. Function with argument and no return type: In this approach, we could make the calling function to read data from the terminal and pass it on the called function.
3. Function with argument and return type: In this, the calling function sends the data to the function and the called function returns the value to the calling function statement. It is a two-way data communication between the calling and called function.
4. Function with no argument and return type: In this function, the function does not take any input from the calling function but it returns value to the calling function.
Ways of passing arguments to functions: There are two different mechanisms to pass arguments to function.
• Pass by value (also called as call by value)
• Pass by reference (also known as call by pointer)
Pass by value: In pass by value, values of actual parameters are copied to the variable in the parameter list of the called function. The called function works on the copy and not on the original values of the actual parameters. This ensures that the original data in the calling function cannot be changed accidently.
Pass by pointer: In pass by pointers (also known as pass by address), the memory addresses of the variables rather than the copies of value are sent to the called function. In this case, the called function directly work on the data in the calling function and the changed value are available in the calling function for its use. Pass by pointers method is often used when manipulating arrays and strings. This method is also used when we require multiple values to returned by the called function.
Passing Array To Functions:
Three Rules to pass an array to a function
1. Function must be called by passing only the name of the array.
2. In the function definition, the formal parameter must be an array type, the size of the array does not need to be specified.
3. The function prototype must show that the argument in a array. Note: When an entire array is passed as an argument , the contents of the array are not copied into the formal parameter array. Instead, information about the addresses of array elements are passed on to the function. Therefore any changes introduced to the array elements are truly reflected in the original array in the calling function. However, this does not apply when an individual element is passed on argument.
1. The function must be called by passing only the array name.
2. In the function definition, we must indicate that the array has two dimensions by including two set of brackets.
3. The size of the second dimension must be specified.
4. The prototype declaration should be similar to the function header.
Passing string to functions The string are treated as character array in C and therefore the rules of passing string to function are very similar to those for passing array to functions.
Recursive: A function is said to be recursive if a statement in the body of the function calls itself. In a recursive function, there must be a reachable condition for its termination; otherwise, the function will be invoked endlessly. Write a program to find the HCF for any two numbe entered by user. #include<stdio.h> #include<conio.h> int hcf(int f,int s) { if(f%s==0) return s; return (s,f%s); } void main() { int a,b; clrscr(); printf(“\nEnter the number:”); scanf(“%d%d”,&a,&b); printf(“The Hcf is %d “,hcf(a,b)) ; getch(); }
Storage classes:
The available storage classes are: 1. Automatic variables 2. External variables 3. Static variables 4. Register variables We explain all of above class of variable in terms of visibility, longevity and scope. The scope of variable determines over what region of the program a variable is actually available for use. The longevity refers to the period during which a variable retain a given value during execution of program (alive). The visibility refers to the accessibility of a variable from the memory.
Automatic variable:
Automatic variable are declared inside a functions in which they are to be utilized. They are created when the function is called and destroyed automatically when the function is exited, hence the name automatic. Automatic variables are therefore private ( or local ) to the function in which they are declared. We may also use the keyword auto to declare automatic variables explicitly.
External variables: Variables that are both alive and active throughout the entire program are known as external variables. They are also known as global variables. Write a program to evaluate the following series using recursive function. 12-22+32-42…n2 #include<stdio.h> #include<conio.h> float sum=0.0; int sign=-1; void add(int n,int i) { int term; if(i<=n) { sign=sign*-1; term=i*i*sign; sum=sum+term; add(n,i+1); } else printf(“\n The sum of series is %f:”,sum); }
Pointer: //refer to book for proper knowledge as this content is from class note slide Declaring Pointer Variables Syntax: data_type *pt_name; This tells the compiler three things about the variable pt_name. 1. This asterisk(*) tells that the variable pt_name is a pointer variable. 2. pt_name needs a memory location. 3. pt_name points to a variable type of data_type. Example: int a=10; int *p=&a; Pointer Expression: C allows us to add integers to or subtract integers from pointers, as well as to subtract one pointer from another. Eg. P1+4,p2-2,p1-p2; In addition to arithmetic operations discussed, pointer can also be compared using the relational operators. The expressions such as p1>p2,p1==p2 and p1!=p2 are allowed. However, any comparisons of pointer that refers to separate and unrelated variables make no sense. Comparisons can be used meaningfully in handling array and string. We may no use pointer in division or multiplication. Eg. p1/p2 or p1*p2 or p1/3 are not allowed. Similarly, two pointer cannot be added. i.e p1+p2 Is illegal. Pointer increment and scalar factor: When we increment a pointer, its value is increased by the ‘length’ of the data type that it points to. This length called the scale factor. Example: #include<stdio.h> #include<conio.h> void main() { int x,*b=&x; int y,*a=&y; int z; clrscr(); printf(“\nEnter the value:”); scanf(“%d”,&x); printf(“\nEnter the value:”); scanf(“%d”,&y); z=x+y-*a**b; printf(“\nThe value of z is:%d”,z); printf(“\nOriginal value of b is %u”,b); b++; printf(“\nChanged value of b is %u”,b); printf(“\nOriginal value of a is %u”,a); a=a+2; printf(“\nChanged value of a is %u”,a);
getch(); } Pointer and Arrays: When an array is declared, the compiler allocate a base address and sufficient amount of storage to contain all the elements of the array in contiguous memory allocation. The base address is the location of the first element (index 0) of the array. The compiler also defines the array name as a constant pointer to the first element. Suppose we declare an array x as follows: int x[5]={1,2,3,4,5} Suppose the base address of x is 1000 then: Element x[0] x[1] x[2] x[3] x[4]
Value Address 1000 1002 1004 1006 1008
