Skip to main content

Functions in C

A function is a self-contained block of program statements that performs a particular task. The concept of C programming is combination of functions and the program execution will begins from the special function called main(). The scanf(), printf() main() etc.. that have been used in programs so far are all functions. They are called library functions and C provides a lot of them.In addition to built in library functions, the programmers can write their own functions and use them.
When there are multiple functions in a program , each function is given different name and a list of parameters. When ever the function is to be called, we refer the function name with suitable list of arguments. The program control will then be transferred to the function and the statements in the function will be executed. After the called function is executed, the control returns to the calling function, the place it was called from.A function can call another function, and that another and so on.
Why Functions
The use of functions provides several benefits.
It makes programs significantly easier to understand and maintain by breaking them up into easily manageable chunks.
Code reuse/Code sharing is the another advantage, where the same code can be used where ever needed. This will avoid the need to have same code replicated at various points and hence make the program small and efficient. A well written function may be reused in multiple programs. C standard library is an example of functions being reused.
Flexible debugging is another advantage because functions make the code shorter and more readable.
Data protection is another feature of functions. The local data described within the functions are not available outside and are used only when they are executed.
Functions form an important part of top-down design and structured programming. Top down design is the act of breaking down a task into manageable parts such that each individual part is relatively simple and the parts can be combined without much difficulty to achieve the original task. Structured programming makes the code more elegant and readable.
Using Functions
The following 3 components are associated with the use of functions
1.The declaration statement ( function prototype declaration) that identifies a function with a name, list of arguments and the type of data returned.
return_data_type function_name(data_type var1,data_type var2,…….);
Eg: int max(int a,int b);  // this function max will return the maximum of 2 integers a and b.a and b are called the arguments of the function.
float average(int n,float a[]); // function average to find average of elements of the array a of size n
Note: if the function does not return any value void has to be mentioned. The default return value of a function is int. Function declaration is needed if the called function is written later. The function name should not match with built in function names.
2.The function definition that consists of a function header identifying the function, followed by the body of the function, which contain the statements to be executed.
int max(int a, int b)
int m;
If ( a>b)
return m;
Note: The job of return statement is to hand over some value from the function to the point where the call was made.

3.The calling statement that invokes the function by either passing or not passing some parameters.
The name, number of arguments and type must match with the function definition.

Arguments are passed by value in C function calls. This means that the local copies of the values of the arguments are passed.

When an entire array is an argument of a function, only the address of the array is passed and not the copy of the complete array. Therefore, when the function is called with the name of the array as argument, address to the first element is handed over to the function. This implies that during its execution, the function has the ability to modify the contents of the array that is specified as function argument. This is an exception to the rule of passing the function arguments by value.

Sample program to find power x^k using a function
#include <stdio.h>
int power(int x, int k)
int r=1,i;
for (i=1;i<=k;i++)
return r;
int main()
Int x,k,p;
printf(“Enter x and k \n”);
printf(“%d power %d= %d”,x,k,p);
Note: The variables used in calling the function are called actual arguments and the variables used in function header are called formal arguments.

Try the following programs using functions

1.Finding biggest of 3 numbers.
2.Converting Fahrenheit to Celsius.
3.Finding square of a number.
4.Average of 3 numbers.
5.Çheck whether a given year is leap year or not.
6.Write a function big() to find the biggest of two numbers and use it to find biggest of three numbers.
7.Sum of the digits of a number.
8.Sort array of integers
9.Add a string to the end of another string with out using library function.
10.copy one string to another with out using library function.
11.Write a function to reverse a string and use it to check whether the given string is palindrome or not.
12.Modify a char in a particular position of a string. Use position and char as argument.
13.Find maximum value in an array
14.Average age of students in a class.
15.Write matrix addition of 3 matrices A,B and C. write functions for reading and printing matrices.
16.Write functions to convert feet to inches and inches to centimeters. Write a program which will read feet and convert it into centimeters using the two functions.
17.Factorial of a given number.
18.Use the factorial function to compute nCr.
19.Use the factorial and power function to compute the series 1-x^2/2!+x^4/4!-x^6/6!...n.


Popular posts from this blog

KTU C programming question paper and evaluation scheme

PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark

2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search.
Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search. Starting at the first item in …