My Subject Blog

Function

Definition :

A function is basically a self – contained block of program statements that
performs a particular task in the program.

It is basically a group of statements used to perform a specific task.
Actually, the concept of function is basically the concept called sub-program.

Syntax :

return_type function_name (datatype var1, datatype var2, … , datatype
varN);
{
executable statement 1 ;
executable statement 2 ;
return expr;
}

Example:

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

Components of a function :

1) Function prototype :
Declaration Specifies function name, arg. Type and return value data type.

2) Function Call :
Causes the function to execute.

3) Function Definition :

The function itself contains the lines of code that is to be executed.


Components of a function
Components of a Function
Categories of C Function:

1) Function with no arguments and no return value :

void printHello()
{
printf("Hello");
}

2) Function with no arguments and a return value :

double getPI()
{
return 3.14;
}

3) Function with arguments and no return value :

void printString(char *str)
{
printf("%s",str);
}

4) Function with arguments and a return value :

int sum(int a, int b)
{
return (a+b);
}

Formal Argument and Actual Argument :


Information will pass to the function via special identifiers or variables called arguments or actual arguments and returned with a return statement.


A parameter or variable defined in function definition or function prototype is called a formal parameter.


Formal Argument and Actual Argument
Formal Argument and Actual Argument
Recursive Function :

Definition :

A recursive function is one that calls itself directly or indirectly to solve a smaller version of its task until a final call which does not require a self-call.


OR

A function that calls itself is called the recursive function.

Characteristics of recursion :

1) Recursion function requires stopping at any certain condition. Otherwise, the recursion process will be infinite.
2) Recursion function prepares the stack every time function calls.

Example:

#include<stdio.h>
int fact(int);

int main()
{
int i=4;
printf("Factorial : %d",fact(i));
return 0;
}

int fact(int n)
{
if(n==1)
{
return 1;
}
return n*fact(n-1); //Recursion Call to Function fact()
}

Application of recursion function :

1) Recursion function used in constructing graph
2) Recursion function used for the tree-like structure also.


Command Line Arguments :

int main(int argc, char *argv[])

The declaration states that :
1) main returns an integer value (used to determine if the program terminates successfully).
2) argc stands for argument counter and it counts the number of argument passed by the user argc must be at least 1.
3) argv stands for Argument vector is an array of character type which hold the argument passed by the user.

Example:

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{
int i=1,max;
max=atoi(argv[1]);
for(i=1;i<argc;i++)
{
if(max<atoi(argv[i]))
{
max = atoi(argv[i]);
}
}
printf("Max value is:%d",max);
return 0;
}

The above program can be executed using DOS prompt.

The first argument must be the name of the file and rest of the argument will be NUMBER because we are finding maximum value from an argument.


The atoi function (stdlib.h) will convert character type to int type. We need to use this function to convert character argument into int type.

Comments

Most Visited Post

C Program to Find Largest Odd Number from Array

C Program Swapping using Bitwise Operator

Followers