Introducing the Function in C

Share this Content

Hey pal.

How are you?

If you want to learn how to write code in “C”, first you have to learn the things used in a “C” code. And functions are one of the most important things in a C program.

That’s why today we’ll talk about Fnctions is C language, their types and use. If I missed something, then please comment me that.

functions in c

So,

What is a Function?

A function is a group of statements used to execute a predefined operation and return a value.

Now, in a C program, there are mainly two types of functions are exists.

  1. Built-in Function
  2. User defined functions

Built-in/library functions:

A built-in function is a predefined function, where the task of that function is also predefined in the Header files. So, we don’t have to define the function.

Example: printf(), scanf(), gets(), getchar(), puts() etc.

These functions are stored in the “C” library also their tasks are fixed. For example, the printf() function only prints the data on the screen, nothing more nothing less.

That’s why we’ve to select the library first. Let’s see an example.

User-defined functions:

It is a type of function where the function works according to the user’s/ programmer’s requirement.

These functions are not predefined and we’ve to assign them the task we want to accomplish by the program.

Now, user-defined functions are basically divided into three parts.

  1. Function Declaration
  2. Function Definition
  3. Function Call

So now let’s discuss these things briefly.

Function Declaration:

When we use or create a user-defined function, we’ve to first declare that function before the main() function. This is called “Function Declaration”. Because a user-defined function that we are going to create on our own is not declared or known to the library files. So before use that function, we’ve to declare that. As simple as that.

Here we’ve to write the function name and the return type of the function.

The syntax is like this-

return_data_type function_name ( data_type arguments);

For example-

int sum(int num1, int num2);

Here, int is the return data type of the function, the sum is the function name and int num1, int num2 are the integer parameters/’datatype argument’.

Function Definition:

As we declared the function, now we have to define that function.

So now we will do that?

We’ve to define the actual body of the function, which means we’ve to define the function. And this is called “Function Definition”.

The syntax of the function definition is like this-

return_data_type function_name (parameter list) { // Statements that we want to do by the code; }

In the syntax, we can see three parts. Let’s know what are they-

  1. return_data_type: It defines what kind of data that function returns. It can be any keyword, like- int, char, float. And in case we don’t need any return from the function, then we’ll simply write void there.
  2. function_name: Clearly you can understand that here we’ll mention our actual function.
  3. parameter: In a function parameters are optional. A function doesn’t need to’ve a parameter. When the function is started, we’re passing a value to the function. This is the actual parameter.
  4. Body: The function body contains a collection of statements that actually defines what the function supposed to do.

Now here is another thing I would like to add.

See a function always returns a value to us. So as we just learned that return_data_type helps us to choose the type of the return value.

Now there are 4 types of functions.

  1. Without return type and parametersvoid main(); See here no value will return as well as no parameters are defined.
  2. With return types and parametersint function(int, int[]); See here we have both the return types and the parameters, means it will return a value after performing the assigned the task.
  3. Without return type but parameters void function(int, int[], char[]);
  4. With return but parameterless int main();

Now, the last part is-

Function Calling:

Now, this is a very important part of a C program,

When we create a new function and assigned work to that function, we’ll have to call that function to perform the defined task.

That’s why it is so important.

When we call a function, the program control is transferred to the called function from the main function. Then the function performs its assigned tasks and when it completes its defined task and its return statement executed, it returns the control to the main function again.

Now to call a function, we simply need to pass the required parameters along with the function name.

Subscribe to Tech Break

Its syntax is-

function_name(parameters)

So, we’ve learned all the three parts of a User-defined function.

Now it is time to see a program that contains all three parts.

Here is an example code.

In this code, we create a function called the max function that will find the maximum value for us.

And also, it’ll take the entry from the users.

Maximum Function:

#include <stdio.h> /* function declaration */ int max(int num1, int num2); int main() { /* local variable definition */ int a, b, ret; printf("Enter the first number"); scanf("%d", &a); printf("Enter the second number"); scanf("%d", &b); /* calling a function to get max value */ ret = max(a, b); printf("Max value is : %dn", ret); return 0; } /* function returning the max between two numbers */ int max(int num1, int num2) { /* local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; }

So here if I enter two values say 1000 and 2000, then it will show the result is 2000.

Let’s try it once.

So you can see it works.

Now there are two types of a function call.

  1. Call by Value
  2. Call by Reference
Call by Value:

#include <stdio.h> /* function declaration */ void swap(int x, int y); int main () { /* local variable definition */ int a,b; printf("Enter the first value"); scanf("%d",&a); printf("Enter the second value"); scanf("%d",&b); printf("Before swap, value of a : %dn", a ); printf("Before swap, value of b : %dn", b ); /* calling a function to swap the values */ swap(a, b); printf("After swap, value of a : %dn", b ); printf("After swap, value of b : %dn", a ); return 0; } void swap(int x, int y) { int temp; temp = x; //save the value of x x = y; // put y into x y = temp; // put temp into y return; }

Call by Reference:

#include <stdio.h> //Function declaration void swap(int *x, int *y); int main () { /* local variable definition */ int a,b; printf("Enter the first value"); scanf("%d",&a); printf("Enter the second value"); scanf("%d",&b); printf("Before swap, value of a : %dn", a ); printf("Before swap, value of b : %dn", b ); /* calling a function to swap the values */ swap(&a,&b); printf("After swap, value of a : %dn", a ); printf("After swap, value of b : %dn", b ); return 0; } void swap(int *x, int *y) { int temp; temp = *x; /* save the value of x */ *x = *y; /* put y into x */ *y = temp; /* put temp into y */ return; }

So this is all about function.

FAQs:

Now lets discuss some FAQs.

  •  

    It is- main().

    Whatever we do in C, we’ve to do it in main() function.

     

     

  •  

    It doesn’t make any changes if we change the position of the main() function.

     

     

  •  

    Main function i.e. main() can be used for only once in a c program. We can use many different functions in a c program where we perform different types of operations. But at the end we have to call those functions in our main() function to complete the code. Because main() function is necessary to complete a code.

     

     

Hope you find all your answer from here.

If you want to know anything else feel free to comment below.

Also if I made any mistake or you want to add something here then please comment below. Let us know your thoughts and suggestions.

Thank you.

Also read-

Recursion of function

Share this Content
Snehasish Konger
Snehasish Konger

Snehasish Konger is the founder of Scientyfic World. Besides that, he is doing blogging for the past 4 years and has written 400+ blogs on several platforms. He is also a front-end developer and a sketch artist.

Articles: 192

Newsletter Updates

Join our email-newsletter to get more insights

Leave a Reply

Your email address will not be published. Required fields are marked *