What is Function in C ?

By Shakib | Date: Tue, May 20, 2025

Ham har C program mein functions ka use karte hai, like main, printf, and scanf. In this blog we will discuss about:

  • Ek function kese design hota hai ?
  • Ek function ko program mein kese integrate kare ?
  • Two ya more than functions ko saath kese rakhe ? aur
  • Wo dono ek dusre se kese communicate karenge ?

C functions two categories mein classified ho sakte hai library functions and user-defined functions. main ek user-defined functions ka example hai. printf aur scanf library functions hai. Ham aur bhi dusre library functions use karte hai jaise sqrt, cos, strcat, etc. The main difference in dono categories mein ye hai ki library functions ham nhi likhte lekin user-defined function ham hi likhte hai program ko develop karte time par. However, ek user-defined function ko ham baad mein library function bhi bana sakte hai.

What is User-Defined Functions ?

Function ek block of statements hota hai jiska use kisi specific work ko karne ke liye hota hai. Ye function user khud design karta hai. For example hame ek calculator develop karna hai to uske liye hame no. of function develop karne padenge jitne bhi ham use karenge like Addition, Subtraction, Multiplication aur Division etc. Har ek function ke liye kuch set of statements likhenge jisse wo function ek specific task ko achieve karega. In functions ko user ko khud se develop karne padte hai isliye inhe User-Defined functions kahte hai.

Need for User-Defined Functions

As we know, main ek special function hota hai. Har program mein ek main function hona zaruri hota hai kioki main function se hi program execute hona start hota hai. Ab yaha problem ye hoti hai ki entire code main function mein write karne se program bahut hi large aur complex ban jata hai jisse debugging, testing aur maintaining bahut hi difficult ho jati hai. Isliye agar ek program ko ham functionally divide kar denge (har task ka ek function bana denge) to har part ko independently code karke aur baad mein ek single unit mein combine kar denge. Aise independently coded programs ko subprograms kehte hai, jo ki samjhne, debug, aur test karne mein bahut hi aasan hote, aur aise subprograms ko hi 'functions' kehte hai. Bahut baar kuch aise operations ya calculations hoti hai jo repeatedly poore program mein karni hoti hai. For example ham permutation aur combination ko find karne ke liye ek program likh rahe hai is program mein bahut baar factorial find karne ki need hogi isliye ham yaha ek factorial find karne ka program develop kar lenge jise program ke har us point par call kar lenge jaha hame need hogi. Ye hamara time aur space dono hi save karega.

Function Defination

Function ki definition hi function ki actual implementation hoti hai. It includes following elements:

  1. function name;
  2. function type;
  3. list of parameters;
  4. local variable declarations
  5. function statements; and
  6. a return statement.

Syntex:

function_type function_name(parameter_list)

{

local variable declaration;

executable statement 1;

executable statement 2;

. . . . .

. . . . .

return statement;

}

Function Header

Function Header mein three parts hote hai: the function type (also known as return type), the function name and the formal parameter list. Function header ke end mein semicolon ki koi need nhi hoti hai.

Name and Type

The function type ye specify karta hai ki kis type ki ye function kiya value return karege (like float or double) jab other function calling karenge. Jab function koi value nhi return karta hai to ham isko void se specify karte hai. The function name ek koi bhi valid C identifier ho sakta hai aur ise bhi wahi rules follow karne padenge jo other variable names follow karte hai. Function ka name always related ho jo task wo function perform kar rahe hai, ye ek good practice hoti hai.

Formal Parameter List

The parameter list mein ham wo variables declares karte hai jo data receive karte hai by the calling program. Ye as input data serve hote hai jo ko koi specified task ko perform karne mein help karte hai. Ye actual input values ko represent karte hai, inhe formal parameter bhi kehte hai.

Example:

float quadratic(int a, int b, int c){...}

double power(double x, int n){...}

float mul(float x, float y){...}

int sum(int a, int b){...}

Yaha par samajhne wali baat ye hai ki yaha paranthesis ke end mein koi semicolon nhi aur declaration variable cannot be combined. That is, int sum(int a, b) is illegal.

Function Body

The function body mein necessary declarations and statements hoti hai jo ki task ko perform karne ke liye required hoti hai. Function body braces mein enclosed hoti hai. Ye three parts contains karta hai.

  1. Local variables jinki function ko need padti hai.
  2. Function ki statements jo functions ke task ko perform karengi.
  3. Ek return statement jo us value ko returns kargi jo function ne evaluate kari hai.

Some Examples of typical definitions are:

float mul(float x, float y)
{
    float result; 
    result = x * y;
    return result; 
}
void sum(int a, int b)
{   
    printf("Sum = %d", a+b);
    return; // optional
}
void display(void)
{ 
    printf("No type, no parameters");
}

Function Calls

Ek function ko call karne ke liye simply uska name aur list of actual parameters (or arguments), pass karne hote hai. Exmaple:

main()
{
int y;
y = mul(10, 5); /* Function Call */
printf("%d ", y);
}

Jab compiler function call wali statement ko execute karega to program control function mul() par transfer ho jayega. Ye function ki statements line by line execute hongi aur value return ho jayegi jab return statement encountered hogi. Ye value y ko assigned ho jayegi.

Function Declaration

Like variables, All functions C program mein zarur declare karne padte hai, un functions ko invoked karne se pehle. Ek function declaration (also known as function prototype ) mein four parts hote hai.

  • Function type (return type).
  • Function name.
  • Parameter list.
  • Terminating semicolon.

Syntex: function_type function_name (parameter list);

Function Declaration bahut similar hoti hai function header line se except the terminating semicolon.

Category Of Functions

C language mein functions 4 categories mein divide hote hain – based on arguments aur return value.

  1. Function with no argument and no return value.
  2. Function with argument but no return value.
  3. Function with no argument but return value.
  4. Function with argument and return value.

1. Function with no argument and no return value

Is type ke function mein na koi argument hota hai, na hi return value.

void greet() {
    printf("Hello, Beyond Man!");
}

Use kab karein?

Jab aapko sirf koi message ya fixed task karwana ho.

2. Function with argument but no return value

Yeh function argument accept karta hai lekin koi value return nahi karta.

Example:

void printSquare(int num) {
    printf("Square is: %d", num * num);
}

Use kab karein?

Jab aapko kisi input par kaam karke sirf output print karna ho.

3. Function with no argument but return value

int getNumber() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    return n;
}

Use kab karein?

Jab function input khud le aur value return kare.

4. Function with argument and return value

Yeh sabse flexible type hota hai. Isme arguments bhi hote hain aur return value bhi.

Example:

int multiply(int a, int b) {
    return a * b;
}

Use kab karein?

Jab aapko inputs pe kaam karke result wapas chahiye.

About the Author

Hi, I'm Shakib Ansari, Founder and CEO of BeyondMan. I'm a highly adaptive developer who quickly learns new programming languages and delivers innovative solutions with passion and precision.

Shakib Ansari
Programming

Comments