• We just launched and are currently in beta. Join us as we build and grow the community.

Introduction to Functions

Shoutbox

Ad Revenue Innovator
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
42
Likes
53
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
Introduction to Functions

In this part you will learn:
1. C syntax
2. Functions
3. Function Calling
4. Function definition
5. Function prototype
6. Showing output

What is a Function?

In C, a function is a group of statements that together performs a task. Every C program has at least one function called the ‘main()’ function. It is the heart of the program. Functions divide your code into separate small parts so that the code is easier to understand and easier to write. Each function performs a specific task based on its usage. The C language relies very much on function. The C standard library provides numerous built-in functions that your program can use.
A function can be of different types. i.e. it can either have a return type, a name and some parameters or it can have no return type ‘ void’ and a name and parameters or it can only have a name but no return type or parameters. There is no restriction on the number of parameters a function can have.
Calculating through Functions
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.

These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.
  1. #include<stdio.h>
  2. #include<conio.h>

  3. int

    AddNumbers(

    int

    m1,

    int

    n1)

    ;
  4. int

    SubNumbers(

    int

    m1,

    int

    n1)

    ;
  5. int

    calculatePower(

    int

    n,

    int

    p)

    ;

When using functions we have to first declare the function prototypes. This declaration is done before we start writing the main function. In the declaration we have to mention the return type of our functions. After that the name of the function whatever we like. But then it should be same everywhere. Just for making the function names significant we write the first letter of the name capital and any other word that comes in the function name.
Then we mention our parameters that will be passed to our function. We have to mention the type of the parameters as well as assign them a name so that we can use these names in the body of the function.
  1. int

    main(

    )
  2. {
  3. int

    m,

    n;
  4. printf

    (

    "Enter the numbers to add and subtract.\n

    "

    )

    ;
  5. printf

    (

    "Number 1:"

    )

    ;
  6. scanf

    (

    " %d"

    ,&

    m)

    ;

  7. printf

    (

    "Number 2:"

    )

    ;
  8. scanf

    (

    " %d"

    ,&

    n)

    ;

  9. int

    ans1=

    AddNumbers(

    m,

    n)

    ;
  10. int

    ans2=

    SubNumbers(

    m,

    n)

    ;

Now we enter our main() function. Here we first declare some integers and take input in them from the user. After taking the input comes our first function call. When we write the name of our function in the main function that is called function calling. When we call a function we send in the variables as parameters that we need to send in order to perform our required task.
When we call a function the control of our program goes inside the function. The body of the function is executed first and then the program returns to the main and the rest of the program is then executed.
Now when we called the function we sent the numbers in the function as parameter the afterward the function returns the answer. Same is the case with the other function call.
  1. int

    num,

    pow

    ;
  2. printf

    (

    "\n

    Enter the number to calculate the power"

    )

    ;
  3. printf

    (

    "\n

    Enter the number :"

    )

    ;
  4. scanf

    (

    " %d"

    ,&

    num)

    ;

  5. printf

    (

    "Enter the power:"

    )

    ;
  6. scanf

    (

    " %d"

    ,&

    pow

    )

    ;

  7. int

    ans3=

    calculatePower(

    num,

    pow

    )

    ;

In this piece of the code we declared two more variables. These variables are for calculating the power of user entered number. We took input the number and the power from the user. Then we called the power function and sent the number and the power as the parameter. This function will calculate the power and return the ans which will be stored in the ans3 varibale.
  1. printf

    (

    "\n

    Addition answer:%d"

    ,

    ans1)

    ;
  2. printf

    (

    "\n

    Subtraction answer:%d"

    ,

    ans2)

    ;
  3. printf

    (

    "\n

    Power answer:%d"

    ,

    ans3)

    ;


  4. getch

    (

    )

    ;

  5. }

At the end of thr program we just printed our three answers on the screen and called the getch() function which pauses the console screen.
  1. int

    AddNumbers(

    int

    m1,

    int

    n1)
  2. {
  3. int

    answer=

    m1+

    n1;
  4. return

    answer;
  5. }

  6. int

    SubNumbers(

    int

    m1,

    int

    n1)
  7. {
  8. int

    answer=

    m1-

    n1;
  9. return

    answer;
  10. }

  11. int

    calculatePower(

    int

    n,

    int

    p)
  12. {
  13. int

    product=

    1

    ,

    i;
  14. for

    (

    i=

    0

    ;

    i<

    p;

    i++

    )
  15. {
  16. product=

    product*

    n;

  17. }

  18. return

    product;
  19. }

Now these are our function definitions. These our written outside the main() function. Function definition contains all the code that needs to be executed when that specific function is called.
The AddNumbers() function was called in main and we sent two variables to it. All this function did was added these numbers and stored them in a new variable and then it returned integer value of the result.
The SubNumbers() function was called in main and we sent two variables to it. All this function did was subtracted these numbers and stored them in a new variable and then it returned integer value of the result.
Same is the case with calculatePower() function. It also calculate the result and returns it.

Execute > compile
then Execute > run
Output
functions.png


Book traversal links for Introduction to Functions

  • ‹ Introduction to 2 Dimensional Arrays
  • Up
  • Learning More About Functions ›


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top