Functions in C Programming
What is a Function?
A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required.
Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by it self. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind
The name of the function is unique in a C Program and is Global. It neams that a function can be accessed from any location with in a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.
Structure of a Function
There are two main parts of the function. The function header and the function body.
int sum(int x, int y)
{
int ans = 0; //holds the answer that will be returned
ans = x + y; //calculate the sum
return ans //return the answer
}
Function Header
In the first line of the above code
int sum(int x, int y)
It has three main parts
- The name of the function i.e. sum
- The parameters of the function enclosed in paranthesis
- Return value type i.e. int
Function Body
What ever is written with in { } in the above example is the body of the function.
Function Prototypes
The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains. The prototype of the function in the above example would be like
int sum (int x, int y);
The only difference between the header and the prototype is the semicolon ; there must the a semicolon at the end of the prototype.
Pages: [Page - 1] [Page - 2] [Page - 3] [Page - 4] [Page - 5]
Tags: Function
Like What you See?
Become one of the regulars by subscribing! You'll be the first to know when we add more great posts just like this. Join up by either RSS Feeds or Email Updates today!
There are 88 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.



































What are the functions in C programming? Write a note with examples.
write a program using one dimensional array that searches a number if it is found on the list of the given 5 input numbers and locate its exact location in the list…..
SAMPLE INPUT/OUTPUT DIALOGUE:
enter a list of numbers:
5 4 8 2 6
enter a number to be searhed: 2
2 is found in location 4
/*this is the program that gives the desired output*/ #include<stdio.h> main() { int num[10],i,n,searchno; clrscr(); printf("enter num to be searched"); scanf("%d",&searchno); printf("enter n"); scanf("%d",&n); printf("enter number in list"); printf("the entered numbers are:"); for(i=0;i<n-1;i++) { scanf("%d",&num[i]); } for(i=0;i<n-1;i++) { if(num[i]==searchno) printf("the searchno is %d , loc is %d",num[i],i); else printf("not found"); break; } getch(); }send important programsin functions in c
#include<stdio.h> #include<conio.h> void main(void) { int hour,min,sec,i; clrscr(); printf("ENTER HOURS:"); scanf("%d",&hour); printf("ENTER MINUTES:"); scanf("%d",&min); printf("ENTER SECONDS:"); scanf("%d",&sec); clrscr(); for(i=1;i<=I+1;i++) { sec=sec+1; if(sec==60) { sec=0; min=min+1; } if(min==60) { min=0; hour=hour+1; } if(hour==24) hour=0; clrscr(); gotoxy(10,25); printf("%02d:%02d:%02d",hour,min,sec); sleep(1); } getch(); }#include<stdio.h> #include<conio.h> void main(void) { int hour,min,sec,i; clrscr(); printf("ENTER HOURS:"); scanf("%d",&hour); printf("ENTER MINUTES:"); scanf("%d",&min); printf("ENTER SECONDS:"); scanf("%d",&sec); clrscr(); for(i=1;i<=I+1;i++) { sec=sec+1; if(sec==60) { sec=0; min=min+1; } if(min==60) { min=0; hour=hour+1; } if(hour==24) hour=0; clrscr(); gotoxy(10,25); printf("%02d:%02d:%02d",hour,min,sec); sleep(1); } getch(); }Detail About Recursion and its Type
Here I am going to give a detail about Recursion in C++.
Definition: Recursion is the process where a function is called itself but stack frame will be out of limit because function call will be infinite times. So a termination condition is mandatory to a recursion.
In C++, Recursion can be divided into two types:
(a) Run- Time Recursion: Normal as in C
(b) Compile- Time Recursion: By using Template
Each of these can be also divided into following types:
1. Linear Recursion
2. Binary Recursion
3. Tail Recursion
4. Mutual Recursion
5. Nested Recursion
1. Linear Recursion: This recursion is the most commonly used. In this recursion a function call itself in a simple manner and by termination condition it terminates. This process called ‘Winding’ and when it returns to caller that is called ‘Un-Winding’. Termination condition also known as Base condition.
Example: Factorial calculation by linear recursion
Run-Time Version
int Fact(long n) { if(0>n) return -1; if(0 == n) return 1; else { return ( n* Fact(n-1)); } }Winding Process:
Function called Function return
Fact(6) 6*Fact(5)
Fact(5) 5*Fact(4)
Fact(4) 4*Fact(3)
Fact(3) 3* Fact(2)
Fact(2) 2* Fact(1)
Fact(1) 1* Fact(0)
Terminating Point
Fact(0) 1
Unwinding Process
Fact(1) 1*1
Fact(2) 2*1
Fact(3) 3*2*1
Fact(4) 4*3*2*1
Fact(5) 5*4*3*2*1
Fact(6) 6*5*4*3*2*1
Compile-Time Version
// template for Base Condition template <> struct Fact<0> { enum { factVal = 1 }; }; template <int n> struct Fact { // Recursion call by linear method enum { value = n * Fact<n - 1>::factVal }; };To test it how it’s working at compile time, just call
cout << Fact<-1>::factVal ;
And compile it then compiler error will come, because no template for -1.
2. Binary Recursion: Binary Recursion is a process where function is called twice at a time inplace of once at a time. Mostly it’s using in data structure like operations for tree as traversal, finding height, merging, etc.
Example: Fibonacci number
Run Time Version Code:
int FibNum(int n) { // Base conditions if (n < 1) return -1; if (1 == n || 2 == n) return 1; // Recursive call by Binary Method return FibNum(n - 1) + FibNum(n - 2); // At a time two recursive function called so // binary }Compile Time Version Code
// Base Conditions template<> struct FibNum<2> { enum { val = 1 }; }; template <> struct FibNum<1> { enum { val = 1 }; }; // Recursive call by Binary Method template <int n> struct FibNum { enum { val= FibNum<n - 1>::val + FibNum<n - 2>::val }; };3. Tail Recursion: In this method, recursive function is called at the last. So it’s more efficient than linear recursion method. Means you can say termination point will come(100%) only you have to put that condition.
Example: Fibonacci number
Run Time Version Code:
int FibNum(int n, int x, int y) { if (1 == n) // Base Condition { return y; } else // Recursive call by Tail method { return FibNum(n-1, y, x+y); } }Compile Time Version Code
template <int n, int x, int y> struct FibNum { // Recursive call By tail method enum { val = FibNum<n-1, y, (x+y)>::val }; }; // Base Condition or Termination template<int x, int y> struct FibNum<1, x, y> { enumPlease help me to know how to make a program in C language in finding the factorial of N using for, while and else in the body statements?
thanks….
——– Original Message ——–
Detail About Recursion and its Type
I would like to know how to write source code for this assignment:
Write a new “Currency Conversion†program, with a title, to accept one input currency, which is error checked as a valid entry, and then display its equivalency in US dollars. For example: 300 Canadian Dollar = 189.19 US Dollar.
i find problems in handling functions and arguments my programs when run give me an error reading declaration syntax error can anyone help me
multi functional programes help alot in easy understanding of the programe and they enable you to break down the programe into smaller parts i myself i enjoy using them in my programes
i have seen passing on constants and variables as to another function from the main but what about when the main passes on a constant or a variable to the other function and then it has ti use it and return a value to the main function, is it possible?
function called min(x,y)that returns the smaller of two double values
.phelp nmn po. my project kme. ang problema ayaw bumlik nung mga value, anong gagawin ko?? my alam po ba kau program. ung pang hyperlink, halimbawa, pag a ung pinundot, pupunta sa new page, mala gnun po.. salamat po sa tutulong, badly needed.. salamat.
Should every C program use return 0;before the closing braces of a block?
can u pls give me some sample program with source code of function?thanks…
type of the funtion should be properly declared
what is type of main() function in c?
is it predefined or userdefined?
if it is predefined then where is it stored in the files of c?