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.
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
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:
curved brackets after the function's name are required. You can pass one or more paramenters to a function as follows:
By default function does not return anything. But you can make a function to return any value as follows:
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 inttype. 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. Declaration and DefinitionWhen a function is defined at any place in the program then it is called function definition. At the time of definition of a function actual logic is implemented with-in the function. A function declaration does not have any body and they just have their interfaces. A function declaration is usually declared at the top of a C source file, or in a separate header file. A function declaration is sometime called function prototype or function signature. For the above Demo() function which returns an integer, and takes two parameters a function declaration will be as follows:
Passing Parameters to a FunctionThere are two ways to pass parameters to a function:
Here are two programs to understand the difference: First example is for Pass by value:
Here is the result produced by the above example. Here the values of a and b remain unchanged before calling swap function and after calling swap function.
Following is the example which demonstrate the concept of pass by reference
Here is the result produced by the above example. Here the values of a and b are changes after calling swap function.
|
Using Functions
Subscribe to:
Post Comments (Atom)
C Tutorials
- C - Programming HOME
- C - Basic Introduction
- C - Program Structure
- C - Reserved Keywords
- C - Basic Datatypes
- C - Variable Types
- C - Storage Classes
- C - Using Constants
- C - Operator Types
- C - Control Statements
- C - Input and Output
- C - Pointing to Data
- C - Using Functions
- C - Play with Strings
- C - Str Datatypes
- C - Working with Files
- C - Bits Manipulation
- C - Pre-Processors
- C - Useful Concepts
0 comments:
Post a Comment