Hello readers, welcome to our blog. In the last article, you had learnt about the Strings In C Programming Language. I hope you all understood how to declare a string in c, how to use string and gets() puts() functions but in today’s article, you will learn how to declare a function in c programming language. So let’s get started.
Topics that we cover in function in c programming language
- Function definition.
- Advantages Of Function.
- Types of functions.
- Functions can be called by 2 type in C language.
Many times your program may have some such statements that require you to execute the compiler again and again. In such a situation, it may take a lot of time to write these statements over and over again. Additionally, the program becomes very big, which makes it difficult to read and understand.
To avoid this situation, C provides a mechanism that is called a function. Any statement that you want to execute many places in the program repeatedly writes them in a block and gives this block a unique name.
After this, every time you want to execute these statements in the program, you call that function through that unique name of the function. All statements are written in the same function as the call are executed.
The name and parameters of the Function (you will read more about them) when you type it is called function declaration. When you type all the statements being executed in the function, it is called function definition. When you use any of the functions in the entire program, it is called a function call.
- Creating functions creates the programmer’s time and memory of the computer.
- The same code can be easily used repeatedly. This increases the code re-usability.
- Program module divides into it so that it can easily be managed and debug
- The readability of the program increases.
These two types of functions are being explained in detail.
Predefined Function in C programming language are those functions which are already provided in the C library. These functions have already been declared and defined. Simply have to include header files to use them.
For example, if you want to use functions such as scanf () and printf () in your program, then you include <stdio> header file in your program. These are both predefined function in c programming language.
Some examples of predefined functions are given below.
- void *malloc()
- int to lower() etc…
Creating and Using Function in C programming language
It’s very easy to create and use functions in C. This process has 3 main steps.
Each of these steps has been explained further. Let’s try to know about them.
- Return-type – What kind of value will return when your function execution is complete, you define it by return type. For example, if you are creating a program of addition which adds 2 whole numbers, then your return type is int.
- Function-name – This is the name of your function. This should be unique in the program. When you call the function, you write this name only.
- List-of-parameters – This is the list of the variables that you will pass when calling the function. For example, if you are creating the 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.
Function definition includes return-type, function-name, and list-of-parameters in the same way as in function declaration. These are followed by statements written in the block of curly brackets that you want to execute.
int add(int a, int b)
c = a+b;
argument-list – arguments are those real values that you pass on when calling functions. These values are assigned to parameters in the function definition. After this, the values are processed and the result is returned.
For example, when you call the addition function, you will pass 2 values such as 2 and 7, instead, they can be passed in variables, which have values stored.
These values will be assigned to variable variables a and b and are processed on these variables within the function. Functions that have parameters defined and if you do not pass arguments when doing a function call then the program gets an error.
For example, if you created the addition of a function, you can call it like this.
Functions can be called by 2 type in C language.
- Call by Value – In this manner values and variables are passed as arguments.
- Call by Reference – In this way, the reference of variables is passed in the form of an argument.
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, in fact, it is not passed in the variable function, but that variable is copied to the value parameter variable and then performing operations on that parameter variable.
For example, if two integer variables x and y pass when calling the addition function of the above given above, then the value of those variables will be copied into the variable variables a and b and returning the results by doing the addition to them. Will go.
Τhis type of function call is called call by value. Īt does not change the value variable of the variable variables, and whatever operation happens it performs only on the parameter variables. Ân example of this is being given below.
#include<stdio.h>int add(int a, int b);
printf(“This addition is performed using function call by value\n”);
result = add(x,y); //Call by value
printf(“Result is : %d”,result);
int add(int a, int b)
This addition is performed using a function call by value
Result is 9
When calling a function in c programming language, instead of passing the actual variables, their address can also be passed. By doing so, the addresses in those variables will be copied into variables and the parameter variable will actually point to the argument variables.
This type of function call is called call by reference. In this type of function call, the address of the actual argument variables is passed. By doing so, if changes in the values of parameter variables in the function, the changes in the values of the argument variables also occur. I mean changes in arguments also change in arguments.
In this type of function call, arguments are passed along with the address of operator (&). It passes the address parameter variables of the operator argument. Apart from this, the parameters in the declaration and definition of the function are defined with the value at (*) operator.
An example of the function call by reference is being given below.
#include<stdio.h>int add(int *s); //Declaring add function with value at operator
printf(“Call by Reference Demo\n”);
add(&s); //Calling add() function by reference
printf(“Sum is %d\n”,s);
int add(int *s) //defining add function with
Call by Reference Demo
Sum is 4
Example of C Functions
int add(int a, int b); /* function declaration */
printf(“This is addition program using function!\n”);
result = add(2,7); /* function call */
printf(“Result is : %d”, result);
int add(int a, int b) /* function definition */
c = a + b;
This is an addition program using the function!
The result is: 9