-->
Search here and hit enter....

Function in C language

Hello Viewer! How are you I hope you well? In the previous lesson, we learn operators in C. in this lesson we'll learn Function in C...



Our 1st question is what do you mean by function?

  • A function is nothing but it is a block of code/set of codes/piece of code that performs a unit task.
  • A function has a name, return type and arguments. ( discussed below )
  • A function can be PREDEFINED and USER DEFINED.
  • Predefined Function is declared in Header File & defined in library files.
  • A function is an operation. once defined and can be used many times. for example: to print any name many times so we use loop function. ( Discussed Later )
Syntax:

 return_type, Function name ( argument list );



Note : Please Check Image uploaded in above section.

return type: it means what value that program return or give after taking input. it can be void, int, char etc.it is necessary to write the return type of the program.
Example:
 1)  Void main()
   {
    --------
    --------
   }
here the return type of this function is void means that function doesn't return any value.( Void means blank ).
argument: it means what value I'm passing through parameters. understand with example
void add( int x )
{
 -------
 -------
}
here the argument is int x. simple arguments mean what value I'm passing through parameters. it can be blanks. example:
void add ()
{
 ----
----
}
here the argument is nothing.



Notes in Function:
1) A program must have at least one function.
2) Function name must be unique.
3) In any program must have the main() function.
4) The function consumes memory only when it is invoked and released from as soon as it finishes it works.


There is mainly a four-way to define a function...

  1. Takes Something Return Nothing
  2. Takes Nothing Return Something
  3. Takes Nothing Return Nothing
  4. Takes Something Return Something


1) Takes Something Return Nothing

It means that type of function that take any argument ( discussed in the above section ) and returns some value. understand with example.
Program :

#include    // header file
#include  // header file
void main()      //  Return Nothing        
{
 int x,y,c;
 printf("\nEnter Two Numbers : "); 
scanf("%d%d", &x,&y);
c= add( x,y );  // Takes Something
printf("\nAdd is %d", c);
getch();
}

int add( int a, int b )
{
  int s;
 s=a=b ;
 return( s ) ;
}

 the main function does not return any value because "void" means blank/nothing and add function take the two value x and y.

2) Takes Nothing Return Something

It means that type of function that takes nothing as an argument but return something. understand with example.

Program :

#include
#include
int add ( void );
int main( ) // Return Something
{
 int c;
c= add( );  // Takes Nothing
printf("\nSum is %d", c);
getch( );
}

int add( )
{
 int a,b,s;
 printf("\nEnter Two Number : ");
 scanf("%d%d", &a,&b);
s=a+b;
return( c );
}

In a main function return type is " int " it means it returns some value and add function argument is blank it means it take nothing as an argument.

3) Takes Nothing Return Nothing

It means that type of function that doesn't take any argument and doesn't return any value.understand with example.

#include
#include
void main( )
{
 add( );
 getch();
}

void add()
{
 int a,b,c;
 printf("\nEnter Two Number : ");
 scanf("%d%d", &a,&b);
c=a+b;
printf("\n%d", c);
}

You can see the return type of main function is void it means nothing and add function doesn't take any arguments.


4) Takes Something Return Something

It means that type of function that takes some value as an arguments and also return something.understand with an example.

#include
#include
int add( int  int );
int main( )
{
 int x,y,c;
 printf("\nEnter Two Number : ");
 scanf("%d%d", &x,&y);
c= add( x, y );
printf("\nSum is %d", c);
getch();
}

int add( int a, int b )
{
 int s;
 s=a+b;
return( s );
}




  • Function declaration is also known as function prototype.
  • Function need to declare before use.
  • Function can be declared locally or globally.
  • At the time of defining function if we passes any value into parenthesis then that type of arguments called " Formal Arguments ".
  • At the time of function calling if we passes any value/arguments into parenthesis then that are called " Actual Arguments "

Benefits of Function:
  • Easy to read.
  • Easy to modify.
  • Avoids rewriting of some code.
  • Easy to Debug.
  • Better Memory utilization.





In this post are so enough 4 today. I hope you like it.



Thank You 4 reading my blog!  if you have any problem or issue with this post please comment/contact.

FB COMMENTS ()