What Is A Function
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 calledarguments specified when the function is called. And the
function either returns some value to the point it was called from or returns nothing.
We can divide a long C program into small blocks which can perform a certain task. A
function is a self contained block of statements that perform a coherent task of same
kind.
Structure of a Function
There are two main parts of the function. The function header and the function body.
Function Header
In the first line of the above code
The only difference between the header and the prototype is the semicolon ; there
must the a semicolon at the end of the prototype.
• ena
o ruffa naomi
September 24, 2010 at 7:42 am
guys, can you help me out with this prob: Make a program that would ask for two numbers then
display their quotient aand modulo without using operators / and %.
Log in to Reply
• vasu
#include<stdio.h>
main()
int num[10],i,n,searchno;
clrscr();
scanf("%d",&searchno);
printf("enter n");
scanf("%d",&n);
for(i=0;i<n-1;i++)
scanf("%d",&num[i]);
for(i=0;i<n-1;i++)
if(num[i]==searchno)
else
printf("not found");
break;
getch();
Log in to Reply
o dillep
April 14, 2009 at 11:27 am
send important programsin functions in c
Log in to Reply
• FAIZAN ([email protected])
April 28, 2008 at 10:40 pm
#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();
Log in to Reply
• FAIZAN ([email protected])
#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();
Log in to Reply
o ilimbek
March 19, 2010 at 5:18 pm
n!/n^n (from n=1 to infinity)
Write a C program which will find the sum of the N-terms of the aboveseries. The program will consist
of a MAIN function and a function.
Within the MAIN function.
. Number of terms N will be read from the standard input.
. N will be passed to the function as an argument.
. Sum of the N terms(return value from the function) will be printed.
Within the function.
. Sum of the N terms of the given series will be calculated and returned to the MAIN function.
Log in to Reply
• Asadullah
{
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
};
};
struct Fact
};
};
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;
// 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
struct FibNum
};
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;
}
Compile Time Version Code
template <int n, int x, int y>
struct FibNum
enum
};
};
{
enum
Log in to Reply
• LeeAnna Lynch
• indu
• thrish
• Anu
• adeth usares
• deepak tapa
• imran
December 14, 2008 at 1:51 pm
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?
Log in to Reply
« Older Comments |
Leave a Reply
You must be logged in to post a comment.
NEWS & UPDATES
Subscribe
DOWNLOADS
TUTORIAL CATEGORIES
• Tutorials
o C Programming Tutorials
o C++ Programming Tutorials
o C# Programming Tutorials
o Object Oriented Programming
o Microsoft Direct-X Programming
o Programming Styles
o Data Structures
• Source Code
o C Programming Source Code
o C++ Source Code
o ASP
o Visual Basic Source Code
o PHP Source Code
o Java
o Java Script
• Blog
• Free Utilities
• Computer Books
o General Books
o Programming Books
o Databases
o Web Design & Development
o Computer Science Books
o Certification Central
o General Software
o Graphics & Illustration
o Hardware
o Networking Books
• FAQs
POPULAR TAGS
• Web Hosting
Latest Offers
• Getting Started with Dreamweaver CS5 and Business Catalyst Free Video
Training Tutorials
• iOS 4: Building Data-Driven Applications Free Video Training Tutorials
• Seven Reasons ECM is Good for Government
• ImageNow & Microsoft SharePoint: 5 Ways They Play Well Together
• Gestion de Aplicaciones Compuestas: Llenar el vacio de la visibilidad de TI en
las aplicaciones compuestas y complejas
Featured Posts