What is a function? The function is collection or group of instruction that performs a certain task. Every program of  C has at least o...

Function or methods

/
0 Comments
What is a function?

  • The function is collection or group of instruction that performs a certain task.
  • Every program of  C has at least one function and that function is main ( ).And main () is the main part of the program. Without main( )  we can't run the program in c.
  • The function contains return type, parameter, and circle bracket ( ).  ( ) it is one kind of identity of function every function has it. 
  • There are three stage 
          1 - Declaration

  • As you know, you have to declare or define a variable before you can use it. This is also true for functions. In C, you have to declare or define a function before you can call it.

          2- Definition

  • A variable declaration is a definition, but a function declaration is not. A function declaration alludes to a function that is defined elsewhere and specifies what kind of value is returned by the function. A function definition defines what the function does, as well as gives the number and type of arguments passed to the function. 
  • A function declaration is not a function definition. If a function definition is placed in your source file before the function is first called, you don't need to make the function declaration. Otherwise, the declaration of a function must be made before the function is invoked.
  • For example, I've used the printf() function in almost every sample program in this book. Each time, I had to include a header file, stdio.h, because the header file contains the declaration of printf(), which indicates to the compiler the return type and prototype of the function. The definition of the printf() function is placed somewhere else. In C, the definition of this function is saved in a library file that is invoked during the linking states.
  • Here data_type_specifier specifies the data type that the function should return. function_name is the function name that should follow the rule of naming in C.


          3- Calling 
  • when a function call is made, the program execution jumps to the function and finishes the task assigned to the function. Then the program execution resumes after the called function returns.
  • Program execution jumps to an invoked function when a function call is made. A function call is an expression that can be used as a single statement or within other statements.
  • Each function is a discrete block of code. Thus, a function defines a block scope. This means that a function's code is private to that function and cannot be accessed by any statement in any other function except through a call to that function. (For instance, you cannot use goto to jump into the middle of another function.) The code that constitutes the body of a function is hidden from the rest of the program, and unless it uses global variables, it can neither affect nor be affected by other parts of the

  • The declaration is when we just create a function                                                          
Syntax:  return-type function-name(parameter);
  •       The Definition is blocking containing instruction            
  Syntax:     return-type function-name( Parameter or argument){                                                                                                //statements;                                                                                                                                       //statements;                                                                                                                                     //statements;                                                                                                                                  }
  • The calling is the stage of function in this we run the function then function execute some task. And calling must be in  main( )  
  • Function Arguments
  • If a function is to accept arguments, it must declare the parameters that will receive the values of the arguments. As shown in the following function, the parameter declarations occur after the function name. 
  • Specifying Return Types
  • A function can be declared to return any data type, except an array or function. The return statement used in a function definition returns a single value whose type should match the one declared in the function declaration.
  • By default, the return type of a function is int, if no explicit data type is specified for the function. A data type specifier is placed prior to the name of a function like this:
  • data_type_specifier function_name();
  • Call by Value, Call by Reference
  • In a computer language, there are two ways that arguments can be passed to a subroutine. The first is call by value. This method copies the value of an argument into the formal parameter of the subroutine. In this case, changes made to the parameter have no effect on the argument.
  • Call by reference is the second way of passing arguments to a subroutine. In this method, the address of an argument is copied into the parameter. Inside the subroutine, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument. 
        Syntax:
              main( ){
            funcion-name(parameter value);
             }
Program:
  • Function with Argument
#include <stdio.h>
#include<conio.h>

void max(int a,int b )/* function declaration */
{
if(a >b){
print(" A is greater");/* defination */
}
else{
printf("B is greater");
 }
}
void main ( ) {
max(2,7);/* function calling*/
getch();
}
  • Output:
   B is greater
                                                     

  • Function no argument and no return type    

#include <stdio.h>
#include<conio.h>

void max( )/* function declaration */
{
print(" function is running");/* defination */
}
void main ( ) {
int a,int b;
a=2;
b=9;
max();/* function calling*/
getch();
}


  • Output
function is running

                           

No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts