C Lab Worksheet 12 C & C++ Functions Part 1 (With No Return Values)
C Lab Worksheet 12 C & C++ Functions Part 1 (With No Return Values)
C LAB WORKSHEET 12
C & C++ Functions Part 1 (with no return values)
1. Understanding the fundamental of and using C/C++ User-defined Functions.
2. Functions and loops.
3. Tutorial references that should be used together with this worksheet are starting from C/C++
function part 1, function part 2, function part 3 and function part 4 and C/C++ main().
Functions are one of the most important construct in C/C++. You may find other term for functions
in other programming languages such as routines and procedures. Functions enable C/C++
programs built in structured manner. Each function will perform specific task. Built and tested
functions can be reusable. Common tasks such as printing to the standard output and reading
from standard input can be created and reused.
Till now you already used the built in or predefined function such as printf() and scanf()/scanf_s().
In this worksheet you will learn about user defined functions that is function created by users.
main() is also function with an exception that it is needed in all C/C++ program as an execution
point. Program execution start at main() function.
Function may receive data and/or may also return data. The received data generally called
parameter or placeholder where as the actual value stored in the placeholder normally
called argument.
For example, consider the following main() program:
// start with main() function...
void main()
{
// calling other functions...
InitializeSettings();
ReadInData();
ProcessData();
PrintReport();
}
We can see that this program is divided into four parts and we can get a general idea of what
those parts accomplish. If we are concerned about a specific part, we can look at the code for
that function and obtain the details. If we dont care, then the coding for that function is not cluttering
up main().
After the initial investment to write and test the function, it can be called over and over again with
only one line of code. The logic appears in only one place rather in several places in the large
program so the code troubleshooting or tweaking and maintenance is easier.
Very large programs can be written by a team of programmers rather than by just one person and
thus the job can be done faster. Each programmer would be assigned certain functions to write.
Of course, this arrangement requires that what each function does, what arguments it received
and what it returns must be documented accurately. Otherwise, there would be
misunderstandings between programmers and how they write the functions. A complete story
about main() can be found in Module Y.
Some Fundamental Definition
The function that is calling another one is called the calling function or caller. The function that
is being called is called called function or callee. Functions may receive arguments
(through parameters) or they may not receive any. They may return only one value or none at all. If
an array is being passed by the calling function, the called function may change it and the change
could affect the original array in the calling function. This is not true of scalars (non arrays).
Example
Create an empty win32 console application named mypointer. Add C++ source file
named mypointersrc and set up your project to be compiled as C code. Build and run the
following program.
#include <stdio.h>
// prototype
void BalanceAvg(int, int);
// main() function
void main(void)
{
// main() local variables...
int Ball1[5] = {500, 200, 400, 100, 700}, // balance for one month
Ball2[5] = {800, 300, 400, 0, 600},
// balance for another...
i; // an array's index
// calling the function
printf("First function call, integer arguments...\n");
BalanceAvg(1000, 200);
// ++i is same as i = i + 1
printf("\nSecond function call, array arguments...\n");
for(i = 0; i <= 4; ++i)
Build, run and show the output for the following example. Answer the questions.
#include <stdio.h>
// this is function prototype, must appear before function definition...
void Funny(void);
// main() function
void main(void)
{
// main() calls Funny()
Funny();
printf("In main() - Yes it is me!\n");
}
// defining Funny...
void Funny(void)
{
printf("In Funny() - Silly me.\n");
}
a.
b.
c.
d.
e.
f.
Name two user-defined functions (function bodies which you have typed)?
Name one predefined/system function (a function which is made available to you by the compiler).
Execution starts at main(). What is the first function that main() calls?
What is the second function that main() calls after the first one is done?
What function does Funny() call?
Number the following events in order from 1 through 3:
______ - Funny() calls printf()
2
3
1
g. Starts from opening brace, { after the main function header, main() and ends with the matched
closing brace, }.
h. Starts from the opening brace, { after the Funny function header, void Funny(void) and ends with
the matched closing brace }
i. No it doesn't because of the void keyword.
j. NO it doesn't because of the void keyword.
k. At the top of program after the #include preprocessor directive.
2.
Add another Funny() after the printf() in main(). Build, run and show the output. Answer
the questions that follow.
#include <stdio.h>
// this is function prototype
void Funny(void);
// main()function
void main(void)
{
// main() calls Funny()
Funny();
printf("In main() Yes it is me!\n");
// another call
Funny();
}
// defining Funny
void Funny(void)
{
printf("In Funny() Silly me.\n");
}
a. Number the events in order from 1 to 5.
_____ - Funny() calls printf() first time
_____ - Funny() calls printf() second time
_____ - main() calls printf()
_____ - main() calls Funny()
_____ - main() calls Funny()
b.
c.
d.
e.
f.
g.
When main() calls Funny(), which function do you think is the calling function?
Which function do you think is the called function?
When Funny() calls printf(), which function do you think is the calling function?
Which function do you think is the called function?
Which function gets called and also calls another function?
When printf() is called from main(), Yes it is me!\n is the argument passed to printf(). What
argument is being passed to printf() from Funny()?
h. When main() calls Funny(), is there an argument passed to Funny()?
Ans:
a.
2
5
3
4
1
b.
c.
d.
e.
f.
g.
h.
3.
Next run the following program, show the output and answer the questions.
a. i is a local variable in main(), which means that only main() has access to it. What is the local
variable in Funny()?
b. main() sets the value of i to 54. Then it was printed. Execution then passed to Funny(). At the
beginning of Funny() was the value of i equal to 54 or just garbage?
c. Funny() set the value of i to -28. After coming back to main(), what was the value of i?
d. Was the i in main() the same as i in Funny()?
Ans:
a. Also an i however this i is different from the i in the main() though they have same name. Both i
have different scope and class storage, that is they are stored at different memory location with
same name.
b. From the program output sample, the value of i is 54.
c. This i value is -28.
d. No they are different.
4.
Show the output for the following codes and answer the questions.
4(a)
#include <stdio.h>
void Funny(void);
void main(void)
{
Funny();
}
void Funny(void)
{
int i;
for(i = 1; i <=3; i = i + 1)
printf("*");
printf("\n");
}
4(b)
#include <stdio.h>
void Funny(void);
void main(void)
{
int i;
for(i = 1; i <=3; i = i + 1)
Funny();
printf("\n");
}
-----------------------------------------------------------
void Funny(void)
{
printf("*");
}
a. How many times was Funny() called in Experiment 4(a)? In
Experiment 4(b).
b. Every time that Funny() was called, printf() was called how many
times in Experiment 4(a)? In experiment 4(b)?
c. In total, how many times was printf() called in Experiment 4(a)? In
Experiment 4(b).
5.
Build, run and show the output for the following program. Then answer the questions.
#include <stdio.h>
void Funny(void);
void main(void)
{
int i;
for(i = 1; i <=3; i = i + 1)
{
printf("In main(), i = %d\n", i);
Funny();
}
}
void Funny(void)
{
int i;
for(i = 1; i <= 2; i = i + 1)
printf("In Funny(), i = %d\n", i);
}
a.
b.
c.
d.
e.
f.
g.
6.
Let us pass arguments (values or data) to Funny(). The difference for this thing when a
function receives an argument are shown in bold, an
integer represented by parameter num instead of void.
#include <stdio.h>
// receiving an integer
void Funny(int);
void main(void)
{
// first call, 4 will go into num
Funny(4);
printf("\n");
// second call, 3 will go into num
Funny(3);
}
// num is a formal parameter, acts as a placeholder.
// the value or argument of num can change...
void Funny(int num)
{
printf("In Funny(), the value of variable num = %d\n", num);
}
a.
b.
c.
d.
e.
f.
g.
h.
i.
j.
k.
Ans:
a.
b.
c.
d.
e.
f.
g.
h.
i.
2 times.
Funny() is called with an argument in experiment 6.
4 was passed.
3 was passed.
An integer 4.
An integer 3.
Yes it is a must.
No, it is an optional.
Yes it is a must and this data type must match with the data type declared in prototype. In this case it
is a int.
j. Yes it is given, a num.
k. ...
7
3
4
5
1
6
2
7.
Try the following program. Show the output and answer the questions.
#include <stdio.h>
// function prototype
void Funny(int);
void main(void)
{
int i;
for(i = 1; i <= 5; i = i + 1)
Funny(i);
}
-------------------------------------------------------------
// function definition
void Funny(int num)
{
// local variable, local to Funny()
int j;
for(j = 1; j <= num; j = j + 1)
printf("j = %d\t", j);
printf("\n");
}
a.
b.
c.
d.
e.
8.
Try next program. Show the output and answer the questions.
#include <stdio.h>
// function prototype
void Funny(int, int);
void main(void)
{
Funny(5, 8);
}
// function definition
void Funny(int num1, int num2)
{
for( ; num1 <= num2; num1 = num1 + 1)
printf("num1 = %d\n", num1);
}
a. When two arguments are passed to a function, should data types be given
for each one in the prototype?
b. In the definition of Funny(), do we need the keyword, int in front of num2 as
well? Try it.
c. main() passes what two arguments to Funny()?
d. In Funny(), how do we know which of the passed values will become num1
and num2?
e. How many times does main() call Funny()? How many times does Funny()
call printf()?
9.
a.
b.
c.
d.
Yes it is a must.
Yes it is a must.
5 and 8.
It is in order. That is the first argument will be passed
to the first parameter and so on. In this case 5 will be
copied and stored in num1 and 8 will be copied and
stored in num2.
e. main() calls Funny() once and Funny() calls printf() 4
times.
In the following program, let pass an array to a function. Show the output and
answer the questions.
#include <stdio.h>
// function prototype
void Wish(int, char[ ]);
void main(void)
{
Wish(5, "Happy");
}
www.tenouk.com
| Main |< More On 2D Array Part 3 | C/C++ Functions Part 2 >| Site Index | Download |
The C & C++ Functions: Part 1 | Part 2 | Part 3
tenouk.com, 2008