TRENDING TOPICS

×

C Functions - Syntax, Declaration

In C, a function is an association of group of statements, which is used to perform a particular task.

Function is the most important section of c programming, so try your best to learn function because it base concept to write well organized program and provides technique to write reusable code. Let’s know c functions topics which we need to learn in a proper sequence.

c functions
  1. Basics concept of function, syntax, declaration and understanding of terms used in function.
  2. Types of Function.
  3. Function Call.
  4. User Defined function & their types.
  5. Concept of recursion function.

Note : We will complete whole function in four pages, so don’t skip any part and study in serial manner.

Function Concept

Let’s assume a group of 12 boys planned for a party, to organize party they divided whole group in three groups and provided unique name for each. In each group there are 4 boys and every group assigned a particular task for example first group’s task is decoration, second group’s task is arranging all required stuff and the last group task is cooking. Note that when all groups will complete their task and they will combine all tasks together it will become a party. Now let’s understand function.

  • In above, each groups of boys can be assume as a function and that group boys can be assume as function statements.
  • Each group have a unique name, so function also have a unique name.
  • All groups task combined makes party, in same way all functions combined makes program.
  • Groups are helpful to perform well organized party, in same way functions are helpful to write well-organized programs.

Function

A function is a block of code that performs a specific task.

C Function Syntax

return_type function_name ( parameter list )
{
   // function body
}

Terms used in C Function Syntax

return_type

This define which kind of value that particular function will return for example int, float, char etc. but sometimes a function does not required to return any value so in this situation the return_type will be void. Note that a function can return only one value.

function_name

User can define any name as function name but the naming should follow the variable naming convention rules. For example c keywords are not allowed as function name, space is not allowed in function name etc.

parameter list

We can pass a list of value to a function and there are called as parameters, but parameters are optional means a function may or may not have parameters. We will discuss more about parameter in just a bit.

function body

The function body contains a collection of statements that define what the function does.

What do you think about output..?

C Function Example

Output

Subtraction is  = 15
Multiplication is = 200

How a C Function Execute

It is the most basic and important thing to understand how a function executes. To understand function execution you should know that main() is also a function ( Library Function ). Let’s know some facts.

  • The main() function is entry point of any c program, i.e. a c program execution starts from main() function.
  • A C program can have only one main() function.
  • A C program can have more than one function.
  • Other c function should be in linked form, either directly from main() function or indirectly linked together function by function.
  • C function executes in same sequence as they called in main() function, however it doesn’t matter in which sequence the functions are defined.

In above example there are two function sub() and mul(), since the function sub() in main() function is called first so it executes first and after it’s execution the function mul() executes.

Understand Function Call

Output

Multiplication is = 200
Subtraction is  = 15

Explanation

This example is same as the previous example just only difference in function calling sequence in main() function, in this mul() function executes first and then sub() because the function mul() called first in main() function and sub() is called after the mul().

Understand Function Call

Output

Sum is = 25
Subtraction is  = 15
Multiplication is = 200

Explanation

  • In above example we have defined 3 functions sub(), sum(), mul().
  • The function sum() is called within the body of sub() function in topmost line.
  • In main() function sub() function is called first, so it will execute first, in sub() function’s body the sum() called as first statement to sum() function body execute first, after that the rest lines of code below sum() function in sub() function will be execute.
  • After the complete execution the program control fall on mul() function in main() function’s scope, now the sum() function will call and the scope of mul() function will execute.
  • After the execution of mul() function, the printf line will execute in main() function which is just below the mul() function call.

Note : Now we hope you have understand how a function call happens, Function makes programming very easy and well organized and easy to understand. The most powerful feature of function is code reusability, we can call same function in program several times according to our program need. In function there is only two thing we need to know in proper way, the first function call, which we have learned recently and the second thing is function return type. So let’s understand what is return type how to use it.

C function return type and return value

Suppose if there is a man who makes knife by using west iron parts. You gave him some broken parts of iron, the man did some function to convert those iron parts in knife and finally he return you a knife, now it depends on use where we keep this knife and how we use the knife to perform some task. So in programming term this knife will act as return of function. Let’s know some fact about function returns.

  • A Function may or may not return a value.
  • To return a value from function we use return keyword.
  • Which kind of value a function will return it depends on function retun_type. For example return type are int, void, float, char etc.
  • A function can return only one value, why only one value we will understand just a bit.
  • If we do not need a return value from a function then we can use return type as void.
  • We need same data type variable to hold return value from the function. for example if a function return type is int then we need a variable of type int to hold the value returned by the function. Let’s understand all these points with the help of example.

Function Return Example

Output

Sum is = 70

Explanation

In above example the function sum() return type is int so it must should return a value of type int, means the return value must be the same type as function return type.

  • When sum() function is called in main() function then sum() function scope executes and it return a value of type int.
  • The function sum() returns an int type value so we need a int type of variable to store that return value from function sum().
  • We already have define a int type variable res, and we are using this variable to store the return value from the function.
  • Now we have variable res, in which there is some value which is returned by sum() function, now we can do anything with this value hence we are printing that value by using printf statement.

Function Return Example

  • In above program the function my_choice() return type is char so it is returing char value.
  • To store return value from function my_choice() we are using a char type variable check_var.
  • The three thing should be same return type, return value data type and variable which is used to store return value of function.

Advertise With Us

We offer sponsored posts and guest posts with do-follow links, back-links, and other advertising services, To advertise email us at "advertise@gdatamart.com"

© 2021 GDATAMART.COM (All Rights Reserved)