|
C Programming Tutorials
Basics of C:
Facts about C
Why to Use C
C Program File
C Compilers
Program Structure:
Simple C Program
C
Program Compilation
Basic DataTypes:
DataTypes
Modifiers
Qualifiers
Arrays
Variable Types:
Local Variable
Global
Variable
Storage Classes:
auto storage class
register storage
class
static storage
class
extern storage
class
Using Constants:
Defining Constants
The enum Data Types
Operator Types:
Arithmetic Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc
Operators
Control Statements:
Branching
Looping
Input and Output:
printf() function
scanf() function
Pointing to Data:
Pointers and Arrays
Pointer
Arithmetic
Pointer Arithmetic
with arrays
Functions:
Using functions
Declaration and
Definition
Strings:
Reading and Writing
Strings
String Manipulation Function
Structured DataTypes:
Structure
Pointer to Structure
Working with Files:
Files
Basic I/O
Bits:
Bits Manipulation
Bits Field
Pre-Processors:
Pre-Processors Examples
Parameterized Macros
Macro
Caveats
Useful Concepts
Built-in Library Functions:
String Manipulation
Function
Memory Management
Function
Buffer
Manipulation
Character
Functions
Error Handling
Functions
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
C Programming Tutorials
Using Functions
A function is a module or block of program code which deals with a particular
task. Making functions is a way of isolating one block of code from other
independent blocks of code.
Functions serve two purposes.
-
They allow a programmer to say: `this piece of code does
a specific job which stands by itself and should not be mixed up with
anything else',
-
Second they make a block of code reusable since a
function can be reused in many different contexts without repeating parts of
the program text.
A function can take a number of parameters, do required
processing and then return a value. There may be a function which does not
return any value.
You already have seen couple of built-in functions like printf(); Similar way
you can define your own functions in C language.
Consider the following chunk of code
int total = 10;
printf("Hello World");
total = total + l; |
To turn it into a function you simply wrap the code in a pair
of curly brackets to convert it into a single compound statement and write the
name that you want to give it in front of the brackets:
Demo()
{
int total = 10;
printf("Hello World");
total = total + l;
} |
curved brackets after the function's name are required. You
can pass one or more parameters to a function as follows:
Demo( int par1, int par2)
{
int total = 10;
printf("Hello World");
total = total + l;
} |
By default function does not return anything. But you can
make a function to return any value as follows:
int Demo( int par1, int par2)
{
int total = 10;
printf("Hello World");
total = total + l;
return total;
} |
A return keyword is used to return a value and datatype of
the returned value is specified before the name of function. In this case
function returns total which is int type. If a function does not return a value
then void keyword can be used as return value.
Once you have defined your function you can use it within a program:
Functions and Variables:
Each function behaves the same way as C language standard function main(). So a
function will have its own local variables defined. In the above example total
variable is local to the function Demo.
A global variable can be accessed in any function in similar way it is accessed
in main() function.
NEXT >> Declaration
and Definition
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
C Aptitude Questions
for more C Aptitude Interview Questions with Answers
Check
C Interview Questions
for more C Interview Questions with Answers
|