Vous êtes sur la page 1sur 32

Functions

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.

First line of function


The general term of first line of functions can be written as: data-type function-name (formal argument 1, formal argument2formal argument n) The formal arguments allow information to be transferred from the calling portion of the program to the function. These formal arguments are called actual parameters when they are used in function reference.

Formal and actual arguments


The parameters in the body of the functions are called actual arguments. The names of actual parameters and formal parameters may be either same or different but their data type should be same.

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; }

maxi function doesnt return anthing so it may be or maynot be present

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

Passing arguments to function


Passing by value the value of the actual argument is copied into the function it prevents information from being transferred back to the calling portion of the program via arguments. Thus, passing by value is restricted to a oneway transfer of information.

Example passing by value


int swap(int a,int b); main() { int a,b; a=5; b=10; printf("% d",a); printf("%d",b); swap(a,b); printf("% d",a); printf("%d",b); getch(); } int swap(int x,int y) { int temp; temp=x; x=y; y=temp; }

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;

If(n>1) For (i=2;i<=n;++i) Prod*=I; Return prod; }

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) {

/* Global variable - static is the default */

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

Points regarding recursion


More memory for more auto variables in stack If not properly checked let the procedure be run out of memory Produces overhead of function calls

Vous aimerez peut-être aussi