lecture notes for c n cpp - functions
lecture notes for c n cpp - functions
When the last entry of a loop is a special value, this is called a sentinel. That is, the whole loop continues to
execute as long as the program do not read the sentinel. Consider the following example:
Scanf(var);
While(var != sentinel)
{scanf(var);
}
Example 2.
//sentinel loop - 10-11-2024
#include <stdio.h>
int main()
{
const int sentinel = -100; //a sentinel special value
int average, num, sum = 0, count = 0;
printf("Enter values you want to add OR end with %d\n",sentinel, " ");
scanf("%d", &num);
while (num != sentinel){
sum = sum +num;
count++;
printf("Enter next values OR end with: %d\n",sentinel);
scanf("%d", &num);
}
if (count !=0){
average = sum/count;}
else{
printf("\nNo input\n");
}
printf("Sum of your Numbers is: %d\n",sum);
printf("Avearge of your Numbers is: %d\n", average);
return 0; //not neceesary needed. the compiler will do the needful
}//end main function
The sample code runs but with errors. What is the error?
Chapter Six
Functions
The C and C++ standard library provides a rich collection of functions for performing common
mathematical calculations, string manipulations, character manipulations, input/output, and many other
useful operations. These are sometimes referred to as programmer-defined functions. Functions are
invoked by a function call, which specifies the function name and provides information (as arguments)
that the function needs to perform its designated task.
Functions allow you to modularize a program. All variables defined in function definitions are local
variables—they can be accessed only in the function in which they’re defined. Most functions have a list
of parameters that provide the means for communicating information between functions. A function’s
parameters are also local variables of that function.
Definition: Functions are used in C to break large computing tasks into smaller units or modules, which
are block of statements separated from the main program to perform a portion of the main task. they can be
inbuilt or standard, programmer or user-defined. A function when declared has a prototype specifying the
function type, name, and parameter list.
6.1 Function Definition
The format of a function definition is
return-value-type function-name (parameter-list)
{
definitions
statements
}
The function-name is any valid identifier. The return-value-type is the data type of the result returned to
the caller. The return-value-type void indicates that a function does not return a value. Together, the return-
value-type, function-name and parameter-list are sometimes referred to as the function header. The
parameter-list is a comma-separated list that specifies the parameters received by the function when it’s
called. If a function does not receive any values, parameter-list is void. A type must be listed explicitly for
each parameter.
Consider a program that uses a function square to calculate and print the squares of the integers from 1 to
10.
1. //Functions - 11-11-2024
2. #include <stdio.h>
3. //you declare a function before the main function
4. int square(int y); //the function prototype
5. // now begin the main fucntion
6. int main(){
a. int count;
b. for(count = 1; count <= 10; count++){
i. printf("%d ", square(count));//function call sqaure(count)
c. }
d. printf("");
e. return 0;
7. }// end main fucntion
Assignment.
Explain the two examples above and give your own opinion.