ADS

Functions | Recursion Function in C Programming | Types of Functions | Calling Function | Function Arguments | C Programming

Functions   

                A function is a self-contained program segment that carries out same specific well defined task.
                In other words, a number of statements grouped into a single logical unit is referred to as a function.
                A function will have carried out its intended action whenever it is accessed (i.e. wherever the function is “called”) from some other portion of the program.
                Some function can be accessed from several different places with in a program. Once the function has carried out its intended action control will be returned to the point from which function was accessed.
                Generally, a function will process information i.e. Passed to it from the 1’calling” function of the program and return a single value.
                Information is passed to the function via special identifiers called “arguments” (also called parameters) and return via the return statement.  
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions.


A function can also be referred as a method or a sub-routine or a procedure, etc.

Types of functions:-        


  1.              Function prototype/declaration
  2.              Function definition
  3.         Called function
  4.        Calling function 
  5.         Arguments/parameters;
  6.         Return type.

Declaration: -

Return type (datatype function name(datatype1, datatype2,………..);
Valid identifiers ();

Definition

A function definition in C programming consists of a function header and a function body. Here are all the parts of a function −
·        Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
·        Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
·        Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
·        Function Body − The function body contains a collection of statements that define what the function does.

Syntax:- 

Return type/data type function name (datatype1 arg1, datatype2 arg2, …..)
{
Statements;
Return statements;
}

Calling Function

While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task.
When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value. 


Syntax:-

Function name (arg1, arg2,………);


Functions can be with:-

  1.  Arguments, return value
  2. Arguments , no return value 
  3. No arguments, return value
  4.  No arguments, no return value

      Function Arguments:-

       If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.
     Formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.

    For Example take this program:-

#include<stdio.h>
int area (int,int);
int main()
{
int len, bred, a;
printf("Enter length of rectangle");
scanf("%d",&len);
printf("Enter breadth of rectangle:");
scanf("%d",&bred);
a=area(len,bred);
printf("The area is %d",a);
return 0;
}
int area(int a, int b)
{int area;
area=a*b;
return area;
}

OUTPUT

Functions in C

Recursion Function

                Recursion is a process by which a function call itself repeatedly until some specified condition has been satisfied.
                The process is used for repetitive computation in which action is stated in terms of previous result.
                In order to solve a problem recursively two condition must be satisfied.
1.       The problem must be written in a recursive form.
2.       The problem statement must include a problem stopping condition.

#Program to calculate factorial recursively
#include
long int fact(int);
int main()
{int n;
printf("Enter number");
scanf("%d",&n);
printf("%d!=%ld\n",n,fact(n));
}
long int fact(int n)
{if(n<=1)
return 1;
else
 return (n*fact(n-1));
}

OUTPUT

Recursion Function in C

Post a Comment

0 Comments