#include <stdio.h>
// Prototype
void prn_message(int k);
int main(void)
{
printf("%s compiled on %s at %s\n", __FILE__, __DATE__, __TIME__);
int N;
int status = 0;
const int nbExpectedValues = 1;
do
{
printf("Please enter an integer: ");
status = scanf("%d", &N);
{
int c;
do
{
c = getchar();
} while (c != '\n' && c != EOF);
}
if (status != nbExpectedValues)
{
printf("\a");
}
} while (status != nbExpectedValues);
// Function call like a procedure
prn_message(N);
return 0;
}
void prn_message(int k)
{
printf("\nYou entered the value %d\n\n", k);
}
#include <stdio.h>
// Prototype/Declaration
int sum(int lowerBound, int upperBound); // With a comma!!!
double mean(int lowerBound, int upperBound);
int main(void)
{
int lower, upper, status;
int total;
const int nbExpectedValues = 2;
do
{
printf("Please enter lower bound and upper bound (in that order): ");
status = scanf("%d %d", &lower, &upper);
{
int c;
do
{
c = getchar();
} while (c != '\n' && c != EOF);
}
} while (status != 2);
// Call sum function
// NOTE that effective/actual parameters and formal parameters may have different names
total = sum(lower, upper);
printf("The sum from %d to %d = %d\n", lower, upper, total);
// We can even make the call of a function in the call of another function
printf("The mean value is = %lf\n", mean(lower, upper));
return 0;
}
// Definition
int sum(int lowerBound, int upperBound) // Header
{ // Body
int sum = 0;
for (int n = lowerBound; n <= upperBound; ++n)
{
sum += n;
}
return sum;
}
// Definition
double mean(int lowerBound, int upperBound)
{
// Check for errors
if (lowerBound > upperBound)
{
return 0; // 0.0 will be identified as an error
}
// NOTE the cast
double mean = sum(lowerBound, upperBound) / ((double)upperBound - lowerBound + 1);
return mean;
}
#include <stdio.h>
// Prototype/Declaration
int func1(int);
int func2(int);
int func3(void);
int func4();
func5(void);
int main(void)
{
// OK
int res = func1(42);
printf("RESULT: %d\n", res);
// UB: func2 has no return statement
res = func2(42);
printf("RESULT: %d\n", res);
// WARNING
res = func3(42);
printf("RESULT: %d\n", res);
// OK ^^'
res = func4(42);
printf("RESULT: %d\n", res);
// OK. Default return type is int
res = func5();
printf("RESULT: %d\n", res);
}
// Definition
int func1(int a)
{
printf("CALLING: ");
printf(__func__);
printf("\n");
return 42;
}
int func2(int a)
{
printf("CALLING: ");
printf(__func__);
printf("\n");
// OUPS
}
int func3(void)
{
printf("CALLING: ");
printf(__func__);
printf("\n");
return 42;
}
int func4()
{
printf("CALLING: ");
printf(__func__);
printf("\n");
return 42;
}
func5()
{
printf("CALLING: ");
printf(__func__);
printf("\n");
return 42;
}