July 4, 2016

Functions in c

C Functions are easy to define and use. We have used functions in every program that we have discussed so far.They have primarily limited to three functions main, printf, scanf
In this chapter we learn in detail about Functions. C Functions can be classified in to two types those are Library functions and user- defined functions.
main is an example of user defined function.

Function is a block of statements which is used to perform a specific task.

Library Function

Library functions are the built-in functionsin c such as puts(), gets(), scanf(), printf(),etc.These rae the functions which already have a definition in header files.

User-defined functions

C allows programmer to define their own function according to their requirement. Syntax of Function
return_type function_name(argument list)
  Block of code;
: return types are the type of data that is returned by the function. If nothing is returned then the return type is void.
argument list
: argument list contains variables names along with their data types. These arguments are inputs for the function.

Declaring a Function

A function Declaration tells the compiler about a function name. The actual body of the function can be seperately. Example:
/* function declaration */
int sum(int num1,int num2)
int main()
int a=10; int b=20; int s; /* calling a function to get sum of the numbers */ s=sum(a,b);
printf("sum of the numbers is: %d\n",s);
return 0;
/* function returning the sum of the two numbers */
int sum(int num1,int num2)
/* local variable declaration */
int result;
result= num1+num2;
reurn result;
Sum of the number is : 30


The function is declared first and then it is defined. The main() calls the function sum() and passes two argument so that called function can perform its defined task and return the result.

Function Parameter

There are two types of arguments/ parameters.
1. Actual parameters 2.Formal Parameters. Actual parameters Are parameters that appear in function calls.
Function_name(actual parameters)
Formal parameters Are parameters that appear in function declarations.
return_type function_name(formal_parameters)
Here we have to remember the following instructions while creating the formal or actual parameters.
  1. Formal parameters are always variables.
  2. Actual parameters need not have to be variables. You can use variables, numbers,expressions, or even function calls as actual parameters.
A function depending on whether arguments are present or not and whether a value is returned or not.For this purpose we have to know category of functions. No we let learn Category of functions.

Category of Functions

  1. Functions with no arguments and no return value.
  2. functions with arguments and no return vales.
  3. Function with arguments and retun vales

No comments:
Write comments