Functions in C Programming – Part 3

Read: Functions in C Programming – Part 2

So far we have learnt about the simplest use of functions in C. In serious C programming functions are not used in that way. We have to make them flexible so that we can customize the results as per our requirements. To make generic function we have to pass some values to them. These values are also called parameters or arguments. Based on these parameter our function should return the value to the calling functions.

To make things a bit clear, we want to make such functions which can communicate to its calling function. And it should return the results as per the customization.

Till now we have used the functions like printf() and scanf() in which unknowingly we have passed some arguments like variable names to print it on the screen. We have to obtain similar results in our function. So today I will tell you about passing the values to the functions.

Passing Values to Functions

Lets understand this concept through a program.

Output

Functions in C Programming - Part 3

Explanation

1. In the statement above main() function I have declared the function multi() by writing the instruction int multi(int , int);

int: It is return type. It means which type of value the function should return to the calling function. In this function I have declared that it will return integer value.

multi: It is the name of the function. You can give any name to this function (valid identifier).

(int,int): These are the number of arguments that I will take from the calling functions. I have declared the data type of two arguments as integer. Here I am taking only two arguments, you can take any number of arguments.

2. It is compulsory to declare the function before using it. So that compiler should understand that we will define some custom functions in it.

3. In the first three statements of main() function I have declared some variables and taken some values in it from the user.

4. Now I have passed two parameters or arguments to the my function multi() with the statement mul=multi(x, y);

Here, multi is the name of the function, (x, y) is the arguments that I am passing to the multi() function. These should be integers because as I have declared in the definition of multi() function that I will receive two integer values in it. mul is the variable which will store the value returned by multi() function.

5. Now the control goes to multi() function and the values of variables x and y will automatically be copied in the a and b variables.

6. Now the multiplication takes place inside the multi() function and the result will be stored in ans integer variable.

7. In the last statement I am returning the value stored in ans variable to the calling function i.e. main(). It is done by using the statement return(ans);. Here return is a keyword that returns a single value. It can be also written as return ans.

8. After returning the value the control will again come back to main(). You must remember that as the return statement is encountered the control immediately come back to calling function.

9. Now in last I am printing the answer using printf() function.

I would recommend you to go through the above at least twice to make your basic concepts clear. It is very necessary to understand this concept before proceeding to the further tutorials. If you are finding difficulty in understanding anything then you can ask your question by commenting below.

4 thoughts on “Functions in C Programming – Part 3”

  1. You said that – “5. Now the control goes to multi() function and the values of variables x and y will automatically be copied in the a and b variables.”

    Here are my 3 questions :-
    1. But how did the values of x & y will be automatically copied to a & b?
    2. And why did not you used x & y even in function definition instead of creating new variables a & b?
    3. What is the difference between parameter and argument (with example) ?

    I did not understand these 3 things so please tell me.
    These are some doubts i need to clear for my basics so i can do better programming. So please tell me answers.

    1. Parameter and argument means same. In C language the internal working is like that when we pass variables while calling function, the values of variables used in calling are automatically copied in the variables used in function definition.

      You can use x and y as variable names in function definition instead of a and b. The scope of variables used in calling of multi() function is local to main() function, while the scope of variables used in multi() definition is limited to itself. In short, variables name in function calling and function definition can be same or different, it doesn’t make any difference in working of program.

      1. Really thanks for your reply.
        Now i understand it properly.
        Your tutorials are really helpful.
        I am studying them with full interest.
        its really better than other websites.

  2. Can we define and declare any function before main function ? if it is so then which one method is efficient above one or this one?

Leave a Comment

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