Learning C/C++ Step-By-Step - Page 6
06. Step-by-Step C/C++ --- C Programming - Functions
Functions
I. Introduction | |
II. Function definition | |
III. Types of functions | |
IV. Built-in functions | |
1. Numeric functions 2. String functions 3. Character Test Functions |
|
V. User-Defined functions | |
1. Simple Functions 2. Function with Arguments 3. Function with Returns 4. Function with Recursion |
|
VI. Pointers and Functions | |
1. Parameter Passing by Reference 2. Call by value 3. Call by Reference |
|
VII. Local Vs Global | |
VIII. Storage Class Specifiers | |
Automatic Storage Class Register Storage Class Static Storage Class External Storage Class |
I. Introduction
Here is a program to print the address of a person twice, which is written in both methods using functions and without using functions. It will demonstrate the advantage of functions.
#include <stdio.h> |
#include <stdio.h> |
II. Function Definition
A statement block, which has ability to accept values as arguments and return results to the calling program. So, A function is a self-contained block of statements that perform a specific task.
III. Types of functions
- Built-in functions/ Library Functions/ Pre-Defined functions |
IV. Library Functions
Library functions are designed by the manufacturer of the software, They were loaded in to the disk whenever the software is loaded.
The following functions are the example of the library functions.
1. Numeric Functions
Function | Syntax | Eg. | Result |
Abs | Abs(n) | abs(-35) | 35 |
ceil | ceil(n) | ceil(45.232) | 46 |
floor | floor(n) | floor(45.232) | 45 |
fmod | fmod(n,m) | fmod(5,2) | 1 |
cos | cos(n) | cos(60) | 0.5 |
sin | sin(n) | sin(60) | 0.866 |
tan | tan(n) | tan(60) | 1.732 |
sqrt | sqrt(n) | sqrt(25) | 5 |
pow | pow(n,m) | pow(2,3) | 8 |
2. String Functions
Functions | Syntax | Eg. |
strlen | strlen(str) | strlen(“Computer”) |
strcpy | strcpy(target,source) | strcpy(res,”Pass”) |
strcat | strcat(target,source) | strcat(“mag”,”gic”) |
strcmp | strcmp(str1,str2) | strcmp(“abc”,”Abc”) |
strrev | strrev(target,scr) | fstrrev(res,”LIRIL”) |
3. Character Test Functions
Function | Description |
isalnum | is a letter or digit |
isalpha | is a letter |
isdigit | is a digit |
iscntrl | is an ordinary control character |
isascii | is a valid ASCII character |
islower | is a lower character |
isupper | is a upper character |
isspace | is a space character |
isxdigit | is hexa decimal character |
There is a huge library of functions available, I have given you a tiny portion of it. For more Library Fuctions refer the Help Manual.
V. User-Defined Functions
The programs you have already seen perform divisions of labor. When you call gets, puts, or strcmp, you don’t have to worry about how the innards of these functions work.
These and about 400 other functions are already defined and compiled for you in the Turbo C library. To use them, you need only include the appropriate header file in your program, “The run-time library,” in the library reference to make sure you understand how to call the functions, and what value (if any) it returns.
But you’ll need to write your own functions. To do so, you need to break your code into discrete sections (functions) that each perform a single, understandable task for your functions, you can call them throughout your program in the same way that you call C library functions.
Steps to implement a function
1. Declaration |
• Every function must be declared at the beginning of the program. • Function definition contains the actual code of execution task. • If a function is defined at the beginning of the program, there is no need of function declaration. |
An example function to demonstrate the implementation
/* 32_egfun.c */ |
User defined functions can be divided in to 4 types based on how we are calling them.
1. Simple Functions 2. Function with Arguments 3. Function with Returns 4. Function with Recursion |
1. Simple Functions
Performs a specific task only, no need of arguments as well as return values
Example of Simple Function
/* 33_line.c */ |
2. Function with Arguments
A function, which accepts arguments, is known as function with arguments.
Eg.
/* 34_argu.c */ |
3. Function with Return values
A function which can return values to the calling program is known as function with return values.
Eg.
/* 35_retu.c */ |
4. Function with Recursion
If a statement within the body of a function call the same function is called ‘recursion’ . Sometimes called ‘circular definition’, recursion is thus the process of defining something in terms of itself.
Examples of Recursive of functions
/* The following program demonstrates function call of itself */
int main( ) The same output can be reached using another function:
void disp( ); |
The program must end at a certain point so the key of the recursion lies on soft interrupt, which can be defined using a conditional statement.
Check the following example:
/* 36_recursion.c */ |
Program to find the factorial of the given number: /* 37_fact.c */ |
To find the factorial of a given number using recursion /* 38_fact.c */ |
VI. Pointers and Functions
Parameter Passing by Reference Call by value Call by Reference |
1. Parameter Passing by Reference
The pointer ca n be used in function declaration and this makes a complex function to be easily represented as well as accessed. The function definition makes use of pointers in it, in two ways
– Call by value - Call by reference |
The call by reference mechanism is fast compared to call by value mechanism because in call by reference, the address is passed and the manipulation with the addresses is faster than the ordinary variables. More ever, only one memory location is created for each of the actual parameter.
When a portion of the program, the actual arguments, calls a function and the values altered within the function will be returned to the calling portion of the program in the altered form. This is termed as call by reference or call by address. The use of pointer as a function argument in this mechanism enables the data objects to be altered globally ie within the function as well as within the calling portion of the program. When a pointer is passed to the function, the address of the argument is passed to the functions and the contents of this address are accessed globally. The changes made to the formal parameters (parameters used in function) affect the original value of the actual parameters (parameters used in function call in the calling program).
Eg.
/* 39_func.c */ |
In the above program, these are totally three ‘printf’ statements, tow in the main() function and one in the function subprogram. Due the effect of first printf statement the value of i is printed as 100. Later function call is made and inside function, the value is altered in and is 1001 due to increment. The altered value is again returned to main() and is printed as 1001.
Hence the output is:
The value is 100 The value in function is 101 The value is 101 |
More about Function Calls
Having had the first tryst with pointers let us now get back to what we had originally set out to learn – the two types of functions calls: call by value and call by reference. Arguments can generally be passed to function in one of the two ways:
a. Sending the values of the arguments b. Sending the addresses of the arguments |
2. Call by Value
In the first method the ‘value’ of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. With this method the changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. The following programming illustrated the Call by Value.
/* 40_callbyvalue.c */ |
The output of the above program would be:
X = 20 y = 10 A = 10 b = 20 |
Note that value of a and b remain unchanged after exchanging the value of x and y.
3. Call by Reference
This time the addresses of actual arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses we would have an access to the actual arguments and hence we would be able to manipulate them. The following program illustrates this fact.
* 41_callbyref.c */ |
The output of the above program would be:
A = 20, b = 10 |
Using call by Reference intelligently we can make a function, which can return more than one value at a time, which is not possible ordinarily. This is shown in the program given below.
/* 42_callbyref.c */ |
And here is the output:
Enter radius of a circle 5 Are = 78.500000 Perimeter = 31.400000 |
Here, we are making a mixed call, in the sense, we are passing the value of radius but, address of area and perimeter. And since we are passing the addresses, any change that make in values stored at address contained in the variables a and p, would make the change effective in main. That is why when the control returns from the function areaperi( ) we are able to output the values of area and perimeter.
Thus, we have been able to return two values from a called function, and hence, have overcome the limitation of the return statement, which can return only one value from a function at a time.
VII. Local Vs Global Variables
According to the Scope of Identifiers Variables are declared as of types.
/* 42_globalid.c */ |
Note: Scope Resolution ( :: ) Operator can be available in C++ only.
VIII. Storage Class Specifiers
Until this point of view we are already familiar with the declaration of variables. To fully define a variable one needs to mention not only its ‘type’ but also its ‘Storage Class’.
According to this section variables are not only have a ‘data type’, they also have a ‘Storage Class’.
Storage Classes are of 4 Types
1. Automatic Storage Class 2. Register Storage Class 3. Static Storage Class 4. External Storage Class |
1. Automatic Storage Class
Keyword | auto | |
Storage | Memory | |
Default Value | Null | |
Scope | Local to the block in which the variable defined | |
Life | Until the execution of its block |
Eg:
/* 43_auto.c */ |
2. Register Storage Class
Keyword | register | |
Storage | CPU Registers | |
Default Value | Null | |
Scope | Local to the block in which the variable defined | |
Life | Until the execution of its block |
Eg:
/* 44_register.c */ |
3. Static Storage Class
Keyword | static | |
Storage | Memory | |
Default Value | Zero | |
Scope | Local to the block in which the variable defined | |
Life | Value of the variable persists between different function calls |
Eg:
/* 45_static.c */ |
4. External Storage Class
Keyword | extern | |
Storage | Memory | |
Default Value | Zero | |
Scope | Global | |
Life | As long as the program’s execution doesn’t come to an end |
Eg:
/* 46_extern.c */ |