Tutorial references

c
c++
c#

Tutorial references

chapters

Function in c


function

Function is a collection of statements. It means function is a block of code.

Each and every c program used at least one function.

Function is used to perform special type work.

C language provides more inbuilt functions. All inbuilt functions are defined by c language. Inbuilt function is called "predefine function". It is also called "library function".

NOTE:- you can learn predefine function or library function in different-different chapters.

That function definition is defined by user(programmer) that is called "User define function". In this tutorial we can learn "User define function".

Types of function

There are three types of function in c language.

   (1) default function

   (2) parameterize function

   (3) return type function

Default function

It is also called non-parameterize function.

default function definition Syntax:

return-type functionName( ){

/* Type here block of code. */

}

return-type: It tells what type value can be return from function. return type is same as data-type like void, int, float, etc.

functionName: It is the name of function. It is given by user(programmer).

Default function calling syntax:

Function is not execute its statements without calling.

functionName();

Example

#include <stdio.h>
#include <conio.h>

void msg(){
 printf("\n This is default function.");
}

void main(){
 clrscr();
 msg(); /*msg() function is calling */
 getch();
}

Run program

Below program calculate odd or even number using function.

Example

fun_default_odd_even

#include <stdio.h>
#include <conio.h>

int num;
void readNum(){
 printf("\n Enter Number: ");
 scanf("%d",&num);
}

void calculation(){
  int res;
 res = num % 2;
 if(res == 0){
 printf("\n %d is even number",num);
}
 else{
 printf("\n %d is odd number",num);
  }
}

void main(){
 clrscr();
 readNum();
 calculation();
 getch();
}

Run program

Parameterize Function

Parameterize function definition syntax:

return-type functionName( data-type variableName1, data-type variableName2,.......... ){

/* Type here block of code. */

}

Parameterize function calling syntax:

functionName(value1, vlaue2,........);

Where you have called your function all parameters receive value from there respectively. You can understand from above syntaxes. variableName1 will receive data from value1 and variableName2 will receive data from value2.

Example

#include <stdio.h>
#include <conio.h>

void getDataByPara(int a, int b, int c){
 printf("\n value of a: %d",a);
 printf("\n value of b: %d",b);
 printf("\n value of c: %d",c);
}

void main(){
 clrscr();
 printf("\n This is parameterize function program.");
 getDataByPara(10, 20, 30); /* 10, 20, 30 goes to a, b, c respectively. */
 getch();
}

Run program

Example

#include <stdio.h>
#include <conio.h>

int s;
void getDataByPara(int a, int b, int c){
  s = a + b + c;
 printf("\n %d + %d + %d = %d",a,b,c,s);
}

void main(){
 clrscr();
 printf("\n This is parameterize function program.");
 getDataByPara(10, 20, 30); /* 10, 20, 30 goes to a, b, c respectively. */
 getch();
}

Run program

Below program checks that a number is divided by 5 or not.

Example

#include <stdio.h>
#include <conio.h>

void divideBy(int a){
 int res;
 res = a % 5;
if(res == 0){
  printf("\n %d is divided by 5",a);
}
else{
  printf("\n %d is not divided by 5",a);
}
}

void main(){
  int v;
 clrscr();
 printf("\n This is parameterize function program.");
 printf("\n Enter value: ");
 scanf("%d",&v);
 divideBy(v);
 getch();
}

Run program

From below program you can understand "Optional Parameter" and "Required parameters".

That parameter do not work without getting data(value) from user that is called "required parameter".It means user do not give value on parameter then compiler returns error.

That parameter works without getting data(value) from user that is called "optional parameter".It means user do not give value on parameter then compiler do not return error.

If user give value for optional parameter then it works on user given value otherwise it works with self value.

Example

#include <stdio.h>
#include <conio.h>

int s;
void getDataByPara(int a, int b, int c=0, int d=0){
  s = a + b + c + d;
 printf("\n sum = %d",s);
}

void main(){
 clrscr();
 printf("\n This is parameterize function program.");
 getDataByPara(10, 20, 30); /* 10, 20, 30 goes to a, b, c respectively. */
 getch();
}

Run program

In above program a and b is required parameter but c and d is optional parameter.


Return

"return" is a keyword. It is used to return value from function.

"return" statement can be use with default function or parameterize function.

A function returns only one value in one time calling.

Mostly programmers use "return" as last statement.

If you have used "void" return-type then your function do not use "return" statement because void return-type do not return any value. Void is called empty return-type or data-type. It means you have used return-type excluding "void" then you can use "return" statement.

Example

#include <stdio.h>
#include <conio.h>

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

void main(){
 int res;
 clrscr();
 printf("\n This is parameterize function program.");
 res = returnVal(10, 20);
 printf("\n result = %d",res);
 getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018