C – Functions

A function in C Language is an organized block of reusable code, which avoids repeating the tasks again and again. If you want to do a task repeatedly in a code, then just make a function, set the task in it, and call the function multiple times whenever you need it.

Create and call a Function

To create a function in C language, set the name of the function followed by parentheses:

A function in C language gets executed when it is called:

Let us now see an example to create a function:

Output

Function Declaration and Definition

In the above example, we saw how to create and call a function. We defined and declared the function as well:

As shown above, a function has two parts:

  • Function Declaration
  • Function Definition

We can also write the above function by separating function definition and declaration. Let us see an example:

Output

Function Parameters

Set the parameters in a C function after the name of the function. For example:

To call a function with a parameter, set the value of the variable while calling, for example:

Therefore, we have passed the rank with the value 5 while calling above.

Let us now see an example:

Output

Multiple Parameters

We can also set more than one parameter in a function. For example:

To call a function with multiple parameters, set the value of the variable while calling, for example:

We have passed the following multiple parameters while calling above:

  • player: Amit
  • rank: 1
  • points: 100

Let us now see an example:

Output

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:


For Videos, Join Our YouTube Channel: Join Now


Read More:

C - User Input
C Recursion
Studyopedia Editorial Staff
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading