Académique Documents
Professionnel Documents
Culture Documents
Contents
explain of function describe access to function define parameters data types specification recursion define storage classes automatic, external, static variables
Modular approach
large program to be broken down into a number of smaller, self-contained components, each of which has some unique, identifiable purpose. the use of a function avoids the need for redundant (repeating) programming of the same instructions. The decomposition of a program into individual program modules is generally considered to be an important part of good programming.
Return statement
Information is returned from the function to the calling portion of the program via the return statement. The return statement also causes control to be returned to the point from which the function was accessed. In general terms, the return statement is written as
return expression
Return
return statement simply causes control to revert back to the calling portion of the program without any information transfer. The point to be noted here is that only one expression can be included in the return statement. Cant write return a,b It is not necessary to include a return statement in a program.
Example
#include <stdio.h> main() { int x,y; maxi(int, int); /*function declaration*/ printf(Enter two integer values); scanf(%d %d &x,&y); maxi(x,y); /*call to function*/ } maxi(x,y) /*function definition*/ int x,y; { int z; z=(x>=y)?x:y; print(\n\n Maximum value %d,z); return; }
Accessing function
A function can be accessed by specifying its name, followed by a list of parameters or arguments enclosed in parentheses and separated by commas. The function call may appear by itself or it may be one of the operands within a more complex expression.
example
#include <stdio.h> main() { int a,b,c; printf(Enter two numbers); scanf(%d%d, &a,&b); c=sum_v(a,b,); printf(\n The sum of two variables is %d\n,,c); } sum_v(a,b) int a,b { int d; d=a+b; return d; } sum is present in c variable through the return statement d
result
A=5, b=10 After A=5,b=10
Call by reference
Function is called using address of actual arguments.
Call by reference
Exchange(p1,p2); Main() Int x,y,*p1,*p2; Scanf(x,);scanf(y); P1=&x; P2=&y; Printf(x,y); Exchange(p1,p2); Printf(p1,p2); } Exchange(int &p3,&p4) {int temp; Temp=*p3; *p3=*p4; *p4=temp; }
output
A=5 b=10 A=10 b=5
recursion
Recursion is a process by which a function calls itself repeatedly. two conditions: problem statement must include a stopping condition at each step program should move near to stopping condition
example
#include <stdio.h> main() { int number; long int fact(int number); printf(Enter number); scanf(%d, & number); printf(Factorial of number is % d\n, fact(number)); } long int fact(int number) { if(number <=1) return(1); else return(number *fact(number-1)); }
Storage classes
Storage class refers to scope of variable within a program. Automatic Static Extern register
automatic
Automatic variables are always declared within a function and are local to the function in which they are declared, that is their scope is confined to that function An automatic variable does not retain its value once control is transferred out of its defining function Not necessary to write auto before decleration
Example
Main() { auto int n; Scanf(%d,&n) Printf(factorial(n));} Int factorial(auto int n) { auto int i; n=I;
Auto prod=1;
External
External variables are not confined to single functions. External variable are recognized globally, that means they are recognized throughout the program, they can be accessed from any function that falls within their scope. an external variable can be assigned a value within one function and this value can be used within another function.(used to transfer information to other function)
External
An external variable declaration must begin with the storage class specifier extern. The declaration of external variables cannot include the assignment of initial values. If an initial value is not included in the definition of an external variable, the variable will automatically be assigned a value of zero.
example
int a; int main() { extern int a; //a=20; printf("\n%d",a); a++;
printf("%d",a); getch(); }
answer
Answer is 0 1 extern int a; indicates that the variable a is defined elsewhere, usually in a separate source code module.
register
register is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location). { e.g register int Miles; } Register should only be used for variables that require quick access - such as counters.
Static
Static variables are defined within individual functions and therefore have a same scope as automatic variables. Static variables retain their values throughout the program. Thus, if a function is exited and reentered later, the static variables defined within that function will retain their former values. Declaration must begin with the static storage class designation
example
display_number() { static int number=2; printf(number=%d\n, number); number++; }
first time display_number is called, it prints the value 2, to which number is initialized. Then number is incremented to 3, and terminates. The second time display_number is called, it prints the value of 3. a static variable is allocated only once, the initialization occurs only during the entire program, no matter how many times the function is called
void func1(void);
static count=10;
main() { while (count--) func1(); getch(); } void func1(void) {
int thingy=5; thingy++; printf(" thingy is %d and count is %d\n", thingy, count); }
output
thingy is 6 and count is 9 thingy is 6 and count is 8 thingy is 6 and count is 7 thingy is 6 and count is 6 thingy is 6 and count is 5 thingy is 6 and count is 4 thingy is 6 and count is 3 thingy is 6 and count is 2 thingy is 6 and count is 1 thingy is 6 and count is 0