Académique Documents
Professionnel Documents
Culture Documents
Objectives
Functions:
Allow reusability of code and structuring of programs.
Provide programmers a convenient way of designing
programs.
A parameter:
Is the data that the function must receive when called from
another function.
May or may not be present in a function.
Of a user-defined function is declared outside the {} of that
function.
Solution:
1. The standard functions used in this program within main() are
as follows:
scanf() – parameters are format of input, and pointers to the
variable(s) in which the input must be stored
fflush() – parameter is stdin
Solution:
1. Yes, because the addresses of the variables are passed in by
using call by reference, the memory locations of the variables
are known to the function. Using pointers to the variables, their
values can be modified.
2. False. Values of variables are copied into the parameters only
in the case of a call by value.
3. a. max()
b. min()
c. iii
d. ii
e. After max() is executed, the values of i and j printed out
would be the same as those entered during execution. After
min() is executed, the value of i would still be the same, but
j would increase by 5 (since b is a pointer to the variable j).
4.main() {
int x, y;
printf(“Enter Number: ”);
scanf(“%d”, &x);
fflush(stdin);
printf(“Enter power raise to : “);
scanf(“%d”, &y);
fflush(stdin);
power(&x, &y); }
power(m,n)
int *m, *n; /* power is pointed to by n,
value is pointed to by m */
{ int i=1,val=1;
while(i++<= *n)
val = val ** m;
printf(“%d the power of %d is %d\n”, *n,*m,
val);}
Solution:
Command-line arguments:
Are the parameters that the main() function can receive from
the command line.
Are passed as information from the OS prompt to a program.
The main() function has 2 arguments, argc and argv.
The format of the main() function with parameters is as
follows:
main(argc, argv)
int argc;
char *argv[];
{
:
}
Here, argc is integer and argv is a character array of
unlimited size (hence [ ] in the declaration).
Solution:
Library functions:
Are also known as built-in functions.
Can be used by including the concerned header files.
Solution:
1. Value returned - 32
Declaration - int x;
2. 846*>
3. False
Conversion functions:
Are available as a part of the standard library.
Are used to convert one data type into another.
The following functions are used to convert a string to a
numeric value:
atoi(): Returns the int type value of a string passed to it
and the value 0 in the case the string does not begin with a
digit.
atof(): Returns the double type value of a string passed to it
and the value 0 in the case the string does not begin with a
digit or a decimal point.
Solution:
1. a. 0
b. 345.000000
Purpose Instruction
Solution:
Solution:
1. a. area, number
b. val
2. (a) Is invalid because the array should be float or double type.
(b) Is invalid because it is declared as auto type.
(c) Is invalid because it is declared as auto type.
(d) Is correct.
.
Solution:
1. In program B, val would be declared as follows:
int val;
calc(){
:}
In program A, the declaration would be as follows:
main()
{ extern int val;
:}
2. a. printout() only (Since it is declared within the function
printout() and hence is not global)
x and numarray (if proper extern statements are coded).
b. False (Since it is declared within the function calc(), and
hence it is not global)
Solution:
Work out your answers. A discussion on these follows in the
Classroom.