Tuesday, September 23, 2014

[Delivery] Exercise function

Bài 1: 1. Write a C program that accepts a number and square the number with the help of a function.


To do this,

a. Declare a function.

b. Accept the number.
c. Pass the number to the function and return the square of that number

Function prototype: int cal_square(int number);


My code + result

Bài 2: Write a C program to find the area and perimeter of a circle.


Function prototype: float areaCircle(float radius);
Function prototype: float perimeterCircle(float radius);

My code + result 



Bài 3: Write a C program to calculate the factorial of an integer

Function prototype:  long factorial(int number);

My code + result 

Bài 4: Write a program with a function that takes two int parameters, adds them together, then returns the sum. The program should ask the user for two numbers, then call the function with the numbers as arguments, and tell the user the sum.

Function prototype: int calSum(int number1, int number2);

My code + result 

Bài học rút ra: 

  • Trước khi đặt 1 hàm mới ta phải đặt một nguyên mẫu ( prototype ) với cấu trúc: type function_name ( type x, type y, ... type n); 
  • Cấu trúc function: 
  • type_specifier function_name (arguments)
    {
    body of the function
    return statement
    }

No comments:

Post a Comment