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 প্রোগ্রামিং (Modular C Programming)

কেবল মাত্র একটি ফাংশন দিয়ে কোনো বড়ো জটিল সমস্যা সমাধানের চেষ্টা করা ভাল প্রোগ্রামিংয়ের পদ্ধতি নয়। সঠিক পদ্ধতি হ'ল সমস্যাটিকে কয়েকটি ছোট ছোট এবং সরল টুকরো করে ফেলা যাতে তা আরও বিশদে বোঝা যায় । তারপরে এই ছোট এবং সরল সমস্যাগুলি সমাধান করার জন্য ছোট ছোট ফাংশন ব্লক তৈরি করা এবং পরে সেগুলি নিয়মানুযায়ী সংযোজিত করা ।

Programming Style

Programming Style

In this section, I'd like to briefly highlight some points that will help you write clean programs that can easily be read, understood, and maintained.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Is the C preprocessor part of the C compiler?

    A No. The C preprocessor is not part of the C compiler. With its own line-oriented grammar and syntax, the C preprocessor runs before the compiler in order to handle named constants, macros, and inclusion of files.

Compiling Your Code Under Conditions

Compiling Your Code Under Conditions

You can select portions of your C program that you want to compile by using a set of preprocessor directives. This is useful, especially when you're testing a piece of new code or debugging a portion of code.