Download c programming function and more Study Guides, Projects, Research C programming in PDF only on Docsity!
Function
- A function is self-contained block of statement that perform a single meaning full task
- A c program is made up of many function and among them that must be main
- If program is complex it divides in number of small program is called function
Classification of function
Function are classified in two types:
1. Library or built function
2. User define function (UDF)
1. Library function
The function which are already written and are provide as a part of language are called library function
Example : -
Printf(),scanf(),getch(),clrscr() etc
2. UDF
the function which are written by programmer for sum specific purpose other than library function is called udf
- each function may contain independent code is known as divide and conquer
Need or benefit of udf
- if a program is to large and complex it divide in function
- in such situation we can write a function which can be use repeatedly which
will help us to save time and minimize the size of programe
- it become easy to find error in the program else it is divide into function
- write once and use multiple time
Defining of function
The general form of a function definition in C programming language is as follows – return type function name (parameter list) { Body of the function }
A function definition in C programming consists of a function header and a function body. Here are all the parts of a function −
- Return Type − A function may return a value. The return type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return type is the keyword void.
- Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
- Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
- Function Body − The function body contains a collection of statements that define what the function does.
Function Declarations A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. A function declaration has the following parts – return type function name (parameter list); function declaration consist of:
- Return type
- Function name
- Parameter and argument
Calling a Function While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task. When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program. To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.
/* Nikhil pandey
if (num1 > num2) result = num1; else result = num2;
return result; }
Function Arguments
If a function is to use arguments, it must declare variables that accept the
values of the arguments. These variables are called the formal
parameters of the function.
Formal parameters behave like other local variables inside the function and
are created upon entry into the function and destroyed upon exit.
While calling a function, there are two ways in which arguments can be
passed to a function –
1. Call by value:-
This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
2. Call by reference:-
This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
Function prototype
1. It tells the return type of the data that the function will return.
2. It tells the number of arguments passed to the function.
3. It tells the data types of the each of the passed arguments.
4. Also it tells the order in which the arguments are passed to the function
Syntax:-
return type function name(data type argument 1, data type argument
Category of UDF
- Function with no argument and no return value
- Function with no argument and a return value
- Function with argument and no return value
- Function with argument and a return value
Example of:-
Function with no argument and no return value
/ /Nikhil pandey
// example of no argument and no return value
#include <stdio.h>
#include<conio.h>
void checkPrimeNumber();
int main()
checkPrimeNumber(); // no argument is passed to prime()
return 0;
// return type of the function is void becuase no value is returned from the
function
void checkPrimeNumber()
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i <= n/2; ++i)
if(n%i == 0)
flag = 1;
// getInteger() function returns integer entered by the user
int getInteger()
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
return n;
Example of:-
- Function with argument and no return value
#include <stdio.h>
void checkPrimeAndDisplay(int n);
int main()
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
// n is passed to the function
checkPrimeAndDisplay(n);
return 0;
// void indicates that no value is returned from the function
void checkPrimeAndDisplay(int n)
int i, flag = 0;
for(i=2; i <= n/2; ++i)
if(n%i == 0){
flag = 1;
break;
if(flag == 1)
printf("%d is not a prime number.",n);
else
printf("%d is a prime number.", n);
Example of:-
- Function with argument and a return value
#include <stdio.h>
int checkPrimeNumber(int n);
int main()
int n, flag;
printf("Enter a positive integer: ");
scanf("%d",&n);
// n is passed to the checkPrimeNumber() function
// the value returned from the function is assigned to flag variable
flag = checkPrimeNumber(n);
if(flag==1)
printf("%d is not a prime number",n);
else
printf("%d is a prime number",n);
return 0;