Jumaat, 2 Mac 2012

Function

Dear students;
Kindly answer all the question below with brief explaination/justification.
Submit to my email aminahpolimas@yahoo.com before 12.00 pm 3 March 2012.
Your submission will be considered as your attendance. Thank you.
Good Luck!
P/S: Please submit softcopy to my email. Dont submit hardcopy.

Section A:

1. When a function is called, what happens to program execution? [3 marks]
 When a function is called, program execution jumps to the function. When    the function returns, program execution returns to the caller at the statement immediately following the function call.

2.What is the difference between an argument and a parameter? [3 marks]
 An argument is a value passed to a function. A parameter is a variable that receives the value.

3. If a function requires a parameter, where is it declared? [3 marks]
A parameter is declared after the function’s name, inside the parentheses.

Section B:
1.Show the two forms of the return statement. [3 marks]
  Here are two forms of return:
    return;
    return value;
2.Can a void function return a value? [3 marks]
No, void functions cannot return values.

3.Can a function call be part of an expression? [3 marks]
A call to a non-void function can be used in an expression. When this happens. The function is executed so that its return value can be obtained.

Section C:
1. What are the main differences between local and global variables? [3 marks]
A local variable is known only within the block in which it is declared. It is created upon entry into its block and destroyed when the block is left. A global variable is declared outside all functions. It can be used by all functions and exists during the entire lifetime of the program.

2. Can a local variable be declared anywhere within a block? [3 marks]
Yes, a local variable can be declared anywhere within a block as long as it is declared before it is used.

3. Does a local variable hold its value between calls to the function in which it is declared?                                                                                                                  [3 marks]
No, local variables are destroyed when the function in which they are declared returns.

Section D:
1. What is a function prototype? What is the purpose of a prototype? [3 marks]
A prototype declares a function’s name, return type, and parameters. A prototype tells the compiler how to generate code when the function is called and ensures that it is called correctly.

2. Aside from main(), must all function be prototyped? [3 marks]
Yes, all functions except for main() must be prototyped.
3.  When you use a standard library function, why must you include its header? [3 marks]
In addition to other things, a header includes the prototype for the library function.

4.       What is the difference between function’s declaration and its definition?

Section E:

1.       #include <iostream.h>

2.       #include <string.h>

3.       void main()

4.       {

5.                   char password [10];

6.             char n[20], name[20];

7.            

8.             cout<<"Please insert  your name:";

9.             cin>>name;

10.                 strcpy(n,name);

11.           cout<<n <<endl;

12.                 cout<<"Please enter your password";

13.                 cin>>password;

14.      

15.                       if (strcmp (password, "12345") == 0)

16.                              cout<<"Access Granted !";   

17.                       else

18.                             cout<<"Access Denied !";     

19.     }

1.    Compile the above code.

2. Build a program that can calculate the volume and area of  the box. This program has 2 functions, which are function for assigning values of length, width and depth, and function to calculate and display the volume and area of the box.(Attach your output console and coding).

Tiada ulasan:

Catat Ulasan