C প্রোগ্রামিংয়ে Function কে কিভাবে কল করবেন ?

Submitted by tushar pramanick on Mon, 02/25/2013 - 10:34

Making Function Calls

Based on what you've learned so far, you can write a C program that calls the integer_add() function to calculate an addition and then print out the result on the screen. An example of such a program is demonstrated in Listing 3.2.

 

TYPE
Listing 3.2. A C program that calculates an addition and prints the result to the screen.


 


  /* 03L02.c: Calculate an addition and print out the result */
  
#include <stdio.h>
  /* This function adds two integers and returns the result */
  int integer_add( int x, int y )
  {
     int result;
     result = x + y;
     return result;
  }

 int main()
 {
    int sum;

    sum = integer_add( 5, 12);
    printf("The addition of 5 and 12 is %d.\n", sum);
    return 0;
 }

 

OUTPUT
The program in Listing 3.2 is saved as a source file called 03L02.c. After this program is compiled and linked, an executable file for 03L02.c is created. On my machine, the executable file is named 03L02.exe. The following is the output printed on the screen after I run the executable from a DOS prompt on my machine:

 

    C:\app> 03L02
    The addition of 5 and 12 is 17.
    C:\app>

 

 

ANALYSIS
Line 1 in Listing 3.2 is a comment about the program. As you learned in Hour 2, the include directive in line 2 includes the stdio.h header file because of the printf() function in the program.

 

Lines 3_9 represent the integer_add() function that adds two integers, as discussed in the previous section.

 

The main() function, prefixed with the int data type, starts in line 11. Lines 12 and 18 contain the opening brace and closing brace for the main() function, respectively. An integer variable, sum, is declared in line 13.

 

The statement in line 15 calls the integer_add() function that we examined in the previous section. Note that two integer constants, 5 and 12, are passed to the integer_add() function, and that the sum variable is assigned the result returned from the integer_add() function.

 

You first saw the C standard library function printf() in Hour 2. Here you may find something new added to the function in line 16. You're right. This time, there are two arguments that are passed to the printf() function. They are the string "The addition of 5 and 12 is %d.\n" and the variable sum.

 

Note that a new symbol, %d, is added into the first argument. The second argument is the integer variable sum. Because the value of sum is going to be printed out on the screen, you might think that the %d has something to do with the integer variable sum. You're right again. %d tells the computer the format in which sum should be printed on the screen.

 

More details on %d are covered in Hour 4, "Data Types and Names in C." The relationship between %d and sum is discussed in Hour 5, "Reading from and Writing to Standard I/O."

 

More importantly, you should focus on the program in Listing 3.2 and pay attention to how to call either a user-generated function or a standard C library function from the main() function.

Comments

Related Items

The main() Function, return statement in C Programming

The main() Function
In line 4 of Listing 2.1, you see this function:

The exit() Function in C Programming

The exit() Function

There is also a C library function, exit(), that can be used to cause a program to end. Because the exit() function is defined in a header file, stdlib.h, you have to include the header file at the beginning of your program।

The void Data Type in C Programming

The void Data Type

You may notice that the void word has been added into the C program in Listing 2.2. void is a keyword for a data type in C. When a void is placed prior to a function name, it indicates that the function does not return a value.

#include Directive and Header Files in C Programming

The #include Directive
Let's now move to line 2 in the C program of Listing 2.1:

Compiling and Linking C Programs

Compiling and Linking