Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts
Friday, February 13, 2015
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.
Lets understand this concept through a program.
Output

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.
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 Funtions
Lets understand this concept through a program.
#include<stdio.h>
int multi(int,int);
void main()
{
int x,y,mul;
printf("Enter two values to multiply them
");
scanf("%d%d",&x,&y);
mul=multi(x,y);
printf("Answer is %d",mul);
}
int multi(int a,int b)
{
int ans;
ans=a*b;
return(ans);
}
Output

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.
Thursday, February 12, 2015
Functions in C Programming Part 1
It’s a good approach if we build a program by dividing it into small modules known as functions. In today’s tutorial, I will tell you about the basic use of functions.
So lets begin our quest to learn functions in C programming. The very first question that will hit your mind should be.
A good C programmer avoids writing the same set of statements repeatedly. Instead of it, a programmer makes a function and writes all the statements there and call that function whenever needed.
There are two types of functions.
Inbuilt Function
These functions are already defined to perform specific task. For example printf() to print value on screen while scanf() to read value. There are many other inbuilt functions.
User-defined function
The functions that are defined by the programmer or user are called as user-defined functions. In this tutorial you will learn how to define and use such functions.
In functions we have three parts.
Function declaration
return_type function_name(argument list);
Function declaration tells the compiler about the value that it will return, the name of the function and the arguments or values that will be passed to the function. Passing the values is optional so you can skip argument list passed. If you don’t want to return any value then just write void instead of return_type.
Function Definition
return_type function_name(argument_list)
{
Body_of_funtion;
. . . . . .
. . . . . .
}
It defines the actual body of the function and the task that it will perform.
Function calling
function_name(argument list);
This statement will call the function and the control of the program will go to body of the function. After executing all the statements in the function it will come back where calling was done.
Lets checkout the simple C program with two functions.
Output

Explanation
While transferring the control from main() function to msg() function, the activity of main() function is temporarily suspended. In our above program main() is calling function and msg() is called function.
Function is one of the most important topics in C programming. You cannot write efficient programs without the proper knowledge of functions in C programming. So I recommend you to go through this tutorial at least once to make everything clear. In the next tutorial I will tell you about the multiple calls within one function.
So lets begin our quest to learn functions in C programming. The very first question that will hit your mind should be.
What are functions in C?
A function is a set of statements which are aggregated to perform some specific task. Generally we use functions to perform basic tasks in a generic way.A good C programmer avoids writing the same set of statements repeatedly. Instead of it, a programmer makes a function and writes all the statements there and call that function whenever needed.
There are two types of functions.
Inbuilt Function
These functions are already defined to perform specific task. For example printf() to print value on screen while scanf() to read value. There are many other inbuilt functions.
User-defined function
The functions that are defined by the programmer or user are called as user-defined functions. In this tutorial you will learn how to define and use such functions.
In functions we have three parts.
Function declaration
return_type function_name(argument list);
Function declaration tells the compiler about the value that it will return, the name of the function and the arguments or values that will be passed to the function. Passing the values is optional so you can skip argument list passed. If you don’t want to return any value then just write void instead of return_type.
Function Definition
return_type function_name(argument_list)
{
Body_of_funtion;
. . . . . .
. . . . . .
}
It defines the actual body of the function and the task that it will perform.
Function calling
function_name(argument list);
This statement will call the function and the control of the program will go to body of the function. After executing all the statements in the function it will come back where calling was done.
Lets checkout the simple C program with two functions.
#include<stdio.h>
//funtion declaration
void msg();
void main()
{
printf("Hello All");
//funtion calling
msg();
}
//funtion definition
void msg()
{
printf("
TheCrazyProgrammer");
}
Output

- As I said in earlier tutorials, main() is also a function. Every C program starts with the main() function. It is also called starting function and we cannot alter the control from it in the beginning. Our above program also starts with main() function.
- In the main() function I have printed the message "Hello All" using printf() function.
- After that I have called the function msg() which is created by me. Carefully look I called the msg() function by writing msg();
- After encountering the call to msg() function, the control shifts to the msg() function.
- Now a message "TheCrazyProgrammer" is printed on the screen.
- Again control reaches to the main() function. As there are no statements left in the main() function. So the program comes to end.
While transferring the control from main() function to msg() function, the activity of main() function is temporarily suspended. In our above program main() is calling function and msg() is called function.
Function is one of the most important topics in C programming. You cannot write efficient programs without the proper knowledge of functions in C programming. So I recommend you to go through this tutorial at least once to make everything clear. In the next tutorial I will tell you about the multiple calls within one function.
Subscribe to:
Comments (Atom)