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

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

ক্লাস 24 : তুমি এখন যে গুলি করতে পারো

CLASS 24: What You Can Do Now

You're now in the last chapter of this book. In this lesson you'll learn more about the C language from the following topics:

C Preprocessor এর ব্যবহার ও উপযোগিতা

In Chapter 2, "Writing Your First C Program," you learned how to use the #include preprocessor directive to include C header files. Since then, the #include directive has been used in every program in this book.

C প্রোগ্রামিং ও অ্যাডভান্স File অপারেশন

In last hour's lesson you learned the basics of reading and writing disk data files. In this lesson you'll learn more about communication with disk data files. The main topics discussed in this hour are

    Random access to files
    Reading or writing binary data

C প্রোগ্রামিং ও File অপারেশন

In Chapter 5, "Reading from and Writing to Standard I/O," you learned how to read or write characters through standard input or output. In this lesson you'll learn to read data from or write data to disk files. The following topics are discussed in this lesson:

Unions: বিসদৃশ Data সংগ্রহের অন্য উপায়

In the previous hour's lesson you learned how to store data of different types into structures. In this hour you'll learn another way to collect differently typed data items by using unions. You'll learn about the following topics in this lesson: