C++ Crash Course: a fast-Paced Introduction


Chapter 1 Revisiting step_function



Yüklə 7 Mb.
Pdf görüntüsü
səhifə55/71
tarix20.09.2023
ölçüsü7 Mb.
#145939
1   ...   51   52   53   54   55   56   57   58   ...   71
C Crash Course A Fast-Paced Introduction by Josh Lospinoso

20
Chapter 1
Revisiting step_function
Let’s look at another example that uses 
step_function
. Listing 1-8 incorpo-
rates variable declarations, function calls, and 
printf
format specifiers.
#include  
u
int step_function(int x) { 
v
--
snip
--
}
int main() { 
w
int num1 = 42; 
x
int result1 = step_function(num1); 
y
int num2 = 0;
int result2 = step_function(num2);
int num3 = -32767;
int result3 = step_function(num3);
printf("Num1: %d, Step: %d\n", num1, result1); 
z
printf("Num2: %d, Step: %d\n", num2, result2);
printf("Num3: %d, Step: %d\n", num3, result3);
return 0;
}
Num1: 42, Step: 1 
z
Num2: 0, Step: 0
Num3: -32767, Step: -1
Listing 1-8: A program that prints the results of applying 
step_function
 to several integers
Because the program uses 
printf

cstdio
u
is included. The 
step 
_ function
v
is defined so you can use it later in the program, and 
main
w
establishes the defined entry point.
N O T E
 
Some listings in this book will build on one another. To save trees, you’ll see the use of 
the 
--snip--
 notation to denote no changes to the reused portion.
Inside 
main
, you initialize a few 
int
types, like 
num1
x
. Next, you pass 
these variables to 
step_function
and initialize result variables to store the 
returned values, like 
result1
y
.
Finally, you print the returned values by invoking 
printf
. Each invoca-
tion starts with a format string, like 
"Num1: %d, Step: %d\n"
z
. There are two 
%d
format specifiers embedded in each format string. Per the requirements 
of 
printf
, there are two parameters following the format string, 
num1
and 
result1
, that correspond to these two format specifiers.


Up and Running

Yüklə 7 Mb.

Dostları ilə paylaş:
1   ...   51   52   53   54   55   56   57   58   ...   71




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin