Dear all,

I am a newbie in C. Just come across function prototype.
I have a task in write a function prototype for:

  • A function that finds the larger of two numbers and returns the result

and i write the following.

#include <stdio.h>
/* Function Definition, I do not know whether it is as simplified as definition only*/
void findbig(int num1, int num2)
        {
    If (num1>num2);
        Printf(“%d”,&num1&“is larger”);
    Elseif (num2<num1);
        Printf(“%d”,&num2&“is larger”);
    Else
        Printf(num1&“is equal to”&num2);
        }
Int main()
        {
        Printf(“Compare the 2 numbers”);
        Printf(“Please enter the first number”);
        Scanf(“%d”,&num1);
        Printf(“Please enter the second number”);
        Scanf(“%d”,&num2);
        }

A function prototype is also called a function declaration. You supplied a function definition. In addition you don't follow the description. You function returns nothing while the description mentions it has to return the largest of the two. The function prototype would be:

// Precondition:  <add>
// Postcondition: <add>
int max (const int a, const int b);

The 'const' keyword is optional but recommended to prevent unintented modification of either of the parameters by accident. The way you comment the function is up to you as well. Some use pre- and postconditions, others use doxygen-style comments while others simply have a short description.

A function prototype is also called a function declaration. You supplied a function definition.

A definition is also a declaration, but a declaration is not always a prototype. A prototype is the very specific declaration without a body that defines parameters (excluding the case of an empty parameter list), also referred to by the standard as a "prototype declaration":

void foo(void); // No arguments
int max(const int a, const int b);

A prototype-like declaration with empty parentheses is not a prototype:

void foo(); // Declaration (unknown arguments), not a prototype

Definitions introduce a declaration, but follow the same rules and only introduce a prototype if the declaration alone would do so:

// Definition (no arguments), also a prototype (no arguments)
void foo(void) { }

// Definition (no arguments), also a declaration (unknown arguments), not a prototype
void bar() { }

kw42chan's code was correct as far as technically introducing a prototype even though it was done through a definition. But I suspect that it's not correct in terms of the spirit of the assignment, which is to write a prototype independent of the definition:

int max(int a, int b); // Prototype

int main(void)
{
    ...
}

int max(int a, int b) // Definition
{
    ...
}
#include <stdio.h>
#include <stdlib.h>
/* Function Definition, I do not know whether it is as simplified as definition only*/
void findbig(int num1, int num2);

int main()
        {
            int num1,num2;
        printf("\nCompare the 2 numbers\n");
        printf("Please enter the first number\n");
        scanf("%d",&num1);
        printf("Please enter the second number\n");
        scanf("%d",&num2);
        findbig(num1,num2);//you must call the function
        return 0;
        }
void findbig(int num1, int num2)
{
    if (num1>num2)
    {
        printf("%d is larger",num1);
    }
    else if (num2>num1)//check the sign that you put,it is wrong sign at first
    {
        printf("%d is larger",num2);
    }
    else
    {
        printf("%d is equal to %d",num1,num2);
    }
        }

hi there,ive change a little bit,there is a mistake on your coding,if you want to use if else statement,you must include this sign {},and and do not include this ; at the end of if else statement.Go check your book or make google search,dont be so lazy if you want to success :)

Dear all,
Thanks for all your input!! I will strive to work hard on trying.
Cheers~ :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.