C language: Chapter 5: Functions and pointers

 Function - Function is a self-contained block of statements that performs a coherent task of some kind.

@program

Main () is the calling function and raj (), mahesh () are the called functions.

Summary: -
  1. C program is a collection of one or more functions.
  2. Any C program contains at least one function.
  3. If there is only one function available in C program, it should be main ().
  4. If there are more than one functions available in C program, then one (one and only one) function should be main (), as programs begins with main ().
  5. There is no limit on the number of functions available in C program.
  6. Each function in a program is called, in the sequence specified by the function calls in the main ().
  7. When the called function has done its things, control returns to the main function. When main function runs out of the functions call, the program ends.

  1. A function gets called when a function name is followed by a semicolon.
  2. A function is defined when a function name is followed by pair of braces included group of statements need to be executed.
  3. Any function can be called from any function. Even main function can be called from any function.
  4. A function can be called any number of times.
  5. The order in which function defined and the order in which they are called need not necessarily be same.
  6. When a function called itself, such process is called recursion.
  7. There are basically two types of functions.
    1. library functions                        printf (), scanf ()
    2. User defined functions.                raj (), Mahesh ()
  8. Library functions are commonly required functions grouped together and stored in what is called a library. The library of the functions are present on the disk and written for us by the people who writes compilers for us. Any compiler comes with a library of standard functions. The procedure for calling both types of functions is exactly the same.
Why there is a need to define and use function?
  1. Writing functions avoid re-writing the same code over and over.
  2. Using functions, it will be easier to write programs and keep track of what they are doing. if operations of the program is divided into several kind of activities and each activity is placed in different function. Each could be written and checked more or less independently.  
  3. Separating the code into modular functions also made the program easier to design and understand.
Passing values between functions
The mechanism by which information is passed to the function is called arguements. Arguments are sometimes also called parameters.

a,b,c are called actual arguments.
x,y,z are called formal arguments.

Any number of arguments can be passed to the function; however the type, number and order must be same.

Instead of using different variables in the called functions we can also use the same variables names a,b,c however compiler would treat them as different variable since they are in different function.

Function prototype declaration-    It is not necessary to mention the variables names while declaring the function prototype. We can use both ways.
int calsum (int x, int y , int z);
int clasum (int , int , int);

There are two methods of declaring the formal arguments.
K&R method (kernigham and Ritichie)
int calsum  (int, int, int);

ANSI method [ Most popular method]
int calsum (int x, int y, int z);

The return statement servers two purposes.
On executing the return statement, it transfers the control back to the calling function.
It returns the value present in the parenthesis to the calling function.

It is not necessary that there is only one return statement should be available in the function. More than one return statements may be present in the funtion.

It is not necessary that return statement should always be present in the called or calling function.



  1. Whenever the control returns from the function some value is definitely returned. If a meaningful value is returned, then it should be accepted in the calling program by equating the called function to some variable.

                        rr = check (num);

  1. All the following are valid returns statements.
            return (a);
            return (23);
            return(12.34);
            return. 
in the last statement since we are not returning any specific values, garbage value is definitely getting returned to the calling function. Parentheses are also dropped in last statement.

  1. if we want called function should not return any value then we should use the keyword void.
            void display ()
{
    printf("\n Heads i win");
    printf("\n Tails you lose");
}

  1. A function can return only one value at a time, so below return statements are invalid.
            return (a,b) ;                //incorrect.
            return (x, 12) ;            //incorrect.

            With the help of pointers we can get around this limitation.

  1. If the value of the formal argument get changed in the calling function, the corresponding change would not have any affect in the calling function.



Scope of variable to the function
Default scope of variable to the function is local in which it is defined.



Scope of variable i and is local is limited to main function.
Scope of variable k is local and is limited to check function.

Calling conventions












Comments

Popular posts from this blog

C language : Chapter 2

C Language: Chapter 4 The case control structure.