• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C/C++ Nikkster's C/C++ Programming School Lesson 3

Nikkster

Programmer
Joined
May 9, 2008
Messages
2,848
Reaction score
9
Location
Confidential
Hi people who likes my tutorials. I hope you have been having a nice weekend, just wanted to say that I've had a quite nice weekend but worked as usual.


Today's subject is "Own Functions". Most of you have always wanted to create your own programs and functions and such nice things and today I'm going to explain some new programming commands and how you can demonstrate and construct and create programs that are based on your own functions that just YOU have created.

My lesson just keeps getting harder and harder, so if you haven't read my previous lessons or don't understand it, please study it first and then come back here.

Okay, so let's start.

Here is the code we are going to study:

Code:
#include <stdio.h>
float max(float x, float y)  /* This is giving you max value */
{
if (x > y)
return (x);
else
return(y);
}
int main()  /* This will count the approximately value and the max-value */
{
float number, sum, highest; /* Remember? We declare the variables */
int ant;
sum = 0;
highest = 0;
ant = 0;
printf("Fill in the numbers. \n");
printf("End with "END OF THE FILE" \n");
while (scanf("%f", &number) == 1) {
ant++;
sum += number;
highest = max(highest, number);
}
printf("Approximate Value: %f\n", sum / ant);
printf("Highest value: %f\n", highest);
}
The program contains two separated functions (max and main). In main, we have the "if" that has been replaced with a call by the function "max".

Code:
Highest = max(highest, number);
The functions has been called with the two arguements "highest" and "number".

In the second row of the program we just created, we will start to define the function "max". This definiation contains the so called protocol for "max". By defining this, we will get the answer that the max is a function which is returning a value of the kind "float" and that "max" has two parameters. x and y and both of the kind "float". These functions are called formal parameters. When the function is being called, the values of these two commands that is being called and executed will be copied to x and its corresponding y. Hope you understood this, otherwise just ask your question in this thread. Now let's continue:

To make sure that the functions are being called correctly, the protocol needs to be known before we can start to call it. When you have a program that endures of more than one function, you will also need to think in which order the functions should be placed in the program source. This involves the head-program "main" should always be placed in the end of your program text (if you use the separated function decleration where the protcol indicates, you can place functions in a better order based on your own functions.

As mentioned before, if you have any questions about this tutorial, please don't hestitate to ask and I'll answer it as soon as possible.

Thanks for reading my tutorial! The lesson 4 will come out soon!

Best Regards,
Nikkster
 
Tab your code!
So annoying looking at untabbed code!

__________________
 
Last edited:
Here is the code, tabbed in Code::Blocks :p.

Code:
#include <stdio.h>
float max(float x, float y)  /* This is giving you max value */
{
    if (x > y)
        return (x);
    else
        return(y);
}
int main()  /* This will count the approximately value and the max-value */
{
    float number, sum, highest; /* Remember? We declare the variables */
    int ant;
    sum = 0;
    highest = 0;
    ant = 0;
    printf("Fill in the numbers. \n");
    printf("End with "END OF THE FILE" \n");
    while (scanf("%f", &number) == 1)
    {
        ant++;
        sum += number;
        highest = max(highest, number);
    }
    printf("Approximate Value: %f\n", sum / ant);
    printf("Highest value: %f\n", highest);
}
 
Tabbed in MS Notepad _/:
Code:
#include <stdio.h>
float max(float x, float y)  /* This is giving you max value */
{
	if (x > y)
		return (x);
	else
		return(y);
}

int main()  /* This will count the approximately value and the max-value */
{
	float number, sum, highest; /* Remember? We declare the variables */
	int ant;
	sum = 0;
	highest = 0;
	ant = 0;
	printf("Fill in the numbers. \n");
	printf("End with "END OF THE FILE" \n");
	while (scanf("%f", &number) == 1) {
		ant++;
		sum += number;
		highest = max(highest, number);
	}
	printf("Approximate Value: %f\n", sum / ant);
	printf("Highest value: %f\n", highest);
}
 
Yeah, that would really help the readers... Just use notepad ++, how hard can it be?

I thought tabbing was for readability ;)

Also
Code:
function max(int x, int y) {
    return (x > y) ? x : y;
}
 
Last edited:
Back
Top