Skip to main content

function in C - Computer science fundamentals tutorial

Functions in C

Functions are subprogram which are used to compute a value or perform a task. They can't be run independently and are always called by main() program or by some other function.

Types of function : - 

1. Library or built in function : - Library function are used to perform  standard operation. Example printf(), scanf(), etc.

2. User defined Function : - User defined function are self contain block of statement which are written by user to compare a value or to perform a task. They can be called by the main program repeatedly as per requirements.

Uses of function : - 
1. Functions are very much useful when a block of sentences has to be executed again and again.

2.When the program is to long or complex function are called to perform each task sequence from  main program.  
3. Functions are also used to reduced the different during debugging a program

Function declaration :- 

type name(arg 1,arg 2,.......,arg n)
{
  (local declaration)
   ............................
   (statement block)
.................................
   return(variable or expression)
}

Calling a function : -

A function is called by calling program using the function with the required number of arguments in parentheses. The function called appears in an assignment statement or in an output statement.

example of C program using function

#include<stdio.h>
void Fact(int);//function prototype declearation
int i,fact=1;//global variable declearation
int main() 
{
int n;
printf("\n enter the  number:");
scanf("%d",&n);
Fact(n);//function call
}
//function definition
void Fact(int n) 
{
for(i=1; i<=n; i++) 
{
fact=fact*i;
}
printf("\n factorial of %d is=%d",n,fact);
}
Program explanation:
1. for example we take 5 as input.
2.we declare the void Fact(int) function globally.
3.after taking function call we give the function definition outside the main function.
4. in function definition for loop started.In for loop i=1 & fact=1 then fact=1. after this loop is continue and incremented by1 until the n is grater than equal to i. 
OUTPUT
**NOTES**
Actual arguments & Formal arguments
the arguments listed in the function calling statement are referred to as Actual arguments.
the arguments listed in the function declaration are referred  to as Formal arguments.  


Recursion
A function calling it self again and again to compute a value is preferred to as recursion or recursive function. Normally a function is called by the main function or by other function but in recursion same function is called by itself repeatedly.
Example of recursion

#include<stdio.h>



main() 

{

int k,f;
int Fact(int);
printf("\n enter the  number:");
scanf("%d",&k);
printf("\n factorial of %d is=%d",k,Fact(k));
}
//function definition
int Fact(int k) 
{
  if(k==0)
  return(1);
  else 
  return(k*Fact(k-1));
}
Output


Uses of Recursive functions: -
1. Recursive functions are retain with less number of statement             compared to function.
2. Recursion is effective where terms are generated successively to      compute value.
3.  Recursion is useful for branching processes recursion helps to         create short code that would other wise be impossible. 
Void function
A function which does not return a value directly to the calling program is referred as a void function. The void function commonly used to perform a task and they declaration. It is declared with the keyword instead of data type of the function.
A void function can also use a return statement without a return value.

Comments

Popular posts from this blog

Solve-write C program to find biggest number from three number by using nested if statement

          C program to find biggest number from three number                                                by using                                                                 nested if statement Problem Description This program takes the 3 numbers and finds the biggest among all. Problem Solution 1. Take the three numbers as input. 2. Check the first number if it greater than other two. 3. Repeat the step 2 for other two numbers. 4. Print the number which is greater among all and exit. Program codes:- here the program of finding biggest of three number run this in your Linux, DEV C++,turbo c++ #include<stdio.h> main() { int num1,num2,num3,big;   ...

Important MCQ of RDBMS( Relational database management system)-FCST

Important MCQ of RDBMS  1. A RDBMS consists a collection of ? a. Tables b. Fields c. Records d. Keys  ANS/- a. table 2. The term attribute refers to a ___________ of a table a. Record b. Tuple c. Column d. Key   ans/- c. Column 3. In relational model, the row of table is known to be ?  a. Relation b. Entity field c. Tuple d. Attribute  ans/- C. Tuple 4. . Address field of a person should not be part of primary key, since it is likely to ? a. Dependent b. Too long c. Changed d. Not changed  ans/- c. Changed 5. The relational model is concerned with ? a. Data structure and Data integrity b. Data Manipulation c. Both a and b d. None of these  ans/- c. Both a and b 6. Which is the false statement from the following ? a. A veiw is a named derived table b. A name relation is variable c. A veiw is a named reation and is virtual d. None of these  ans/- d. None of these 7. The union of primary key...

Short notes on database schema & database independence- computer science fundamentals tutorial

            Database Schema &  Data Independence PDF version of this post also available. to download the click the link given at the end of this post.           Database Schema : -  A database schema   is the skeleton structure that represents the logical view of the entire database. It defines how the data is organized and how relat ion among them are associated.  Database schema  formulates all the constrains that are to be applied on the data. A database schema defines its entities and the relationship among them. It contains descriptive details of the database which can be described by means of schema diagram. A database schema can be divided into two categories.   I.     Physical database schema : - this schema contain to the actual storage of data and its form of storage like files, indices etc. It defines how the data will be stored in a secondary storage. ...