Video courses for company/skill based Preparation
Purchase mock tests for company/skill building
Functions in C
Function in C
A function in C is a block of code which is used to perform specific tasks as many times as it is called, and is used to make coding simpler and convenient using a group of statement and operations.
C function, this is a set of statements. there is a function in every single program. where function is required, function is called.
Note: int main() is also a function, every code has at least one function.
Advantages of function:-
- The code written in the function does not have to be written repeatedly.
- function protects programmer’s time and program space.
- Large program can be divided into small functions.
- The function can be called repeatedly where needed.
- The readability of the program increases.
In C language function are two types:-
- Predefined function
- User-defined function
- In-built functions are also called predefined or Library functions.
- In-built functions, different header files or processors have been created for each functions.
- There are lots of header files in C and they are grouped by different functions.
- If programmer wants to create your own header files too.
- Function declaration and definition is in header files.
User defined functions are those functions which programmer (you) create yourself.Programmer can create as many functions as per your requirement. How these functions are created and how they are used.
Creating and using C functions
Declaration of a function
When writing a function we need to tell the computer the name, the return type and the parameter(if any) of that function.
The general Syntax of function declaration:-
return-type – What kind of value will return when your function execution is complete, you define it by return type. If you are creating an addition program which adds 2 whole numbers then your return type is int.
function-name – this is the name of your function. These should be unique in the whole program. When you call the function, you write this name only.
list-of-parameters – These are the lists of variables that you will pass when calling the function. For example, if you are creating addition of function then you can pass 2 numbers or 2 variables as parameters, and then add them inside the function and show results. It is not necessary that you define parameters in all functions.
One thing you should always remember is that the function declaration statement is terminated from semicolon. But this does not happen with the function definition.
Function definition includes return-type, function-name, and list-of-parameters as in the function declaration. After that, those statements are written in the block of curly brackets that you want to execute.
Let’s see how
int add(int a,int b) //a,b are the parameters which can be provided while calling the function
return c;//returns the integer value of the sum here
Whenever you want to use the function in the program, you call it.
argument-list – arguments are those real values that you pass when you call functions. These values are assigned to parameters in the function definition. After this, these values are processed and the result is returned.
In C language, we can call the function is of two types:-
Call by Value – In this manner, values and variables are passed as arguments.
Call by Reference – In this way the reference of the variables is passed in the form of a argument.
Calling a function by value
When you pass a value in the form of an argument while calling the function, it gets copied into the parameter variables and after that the operations are performed on those variables.
Similarly, when you pass a variable while calling the function, it is actually not passed in the variable function, but that variable value is copied to the parameter variable and then performing operations on that parameter variable .
Such a function call is called call by value. It does not change the value of the actual argument variables, and whatever operation happens it performs only on the parameter variables
When the code has been written we need to call the function when and where we want to execute it.Only after calling the function the function will get executed.Let’s learn how in an example
int add(int a,int b)
z=add(x,y); //calls the function and provide the parameter as x and y
printf("The sum of %d and %d is %d",x,y,z);
Note : The original values inside the main functions can’t be changed or modified in any other function but the main function. When we call the function add and give the parameters we only passed a copy of the values and not the original variable itself.