Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Adding 2 functions to a C Program

+12 votes
asked Nov 8, 2022 by Jon Tompkins (220 points)

Hello,

   I need to add two functions to this C Program here: https://onlinegdb.com/uqjIhF5AH

I am trying to do it, but can't get it to work. I want to add the following functions:

int main()
{
    float celsius, fahrenheit;
 
    printf("Please Enter the temperature in Fahrenheit: \n");
    scanf("%f", &fahrenheit);
 
    // Convert the temperature from f to c formula
    celsius = (fahrenheit - 32) * 5 / 9;
    //celsius = 5 *(fahrenheit - 32) / 9;
    //celsius =(fahrenheit-32) * 0.55556;

printf("\n %.2f Fahrenheit = %.2f Celsius", fahrenheit, celsius);
 
    return 0;
}

2nd Function:

int main()
{
  printf("\n Hello World");
  return 0;
}
Thank you for the help.
Jon

3 Answers

+2 votes
answered Nov 9, 2022 by Peter Minarik (86,040 points)

You cannot have more than one main() function (actually, you cannot have more than 1 function with the same name) in a C program.

The main() function is the entry point of your application. Create new functions and put the functionality there. E.g.:

int MyFunction1()
{
    ...
    return 42;
}

void MyFunction2(int value)
{
    ...
}

int main()
{
    int number = MyFunction1();
    MyFunction2(number);
    return 0;
}
+1 vote
answered Nov 15, 2022 by vaibhav (310 points)
/* C Program to Calculate Square, Square root, Triangle Perimeter and Grade Average */

#include<stdio.h>

#include<math.h>

int Calculate_Square (int Number);

float Calculate_Perimeter_Triangle (float a, float b, float c);

float Calculate_Grade (float grade1, float grade2, float grade3,

       float grade4);

int

main ()

{

  int number, Square;

  float a, b, c, Triangle, Square_Root;

  float grade1, grade2, grade3, grade4, Grade;

  printf (" \n Please Enter any integer Value : ");

  scanf ("%d", &number);

  Square = Calculate_Square (number);

  printf ("\n Square of a given number %d = %d\n", number, Square);

/* Square Root */

  printf ("The square root of your number %d is %f\n", number, sqrt (number));

  printf ("\nPlease Enter three sides of triangle\n");

  scanf ("%f%f%f", &a, &b, &c);

  printf ("Enter 4 grade values for your course:\n");

  scanf ("%f%f%f%f", &grade1, &grade2, &grade3, &grade4);

  Grade = Calculate_Grade (grade1, grade2, grade3, grade4);

  printf ("Your grade average is %f\n", Grade);

/* Triangle */

  Triangle = Calculate_Perimeter_Triangle (a, b, c);

  printf ("Area of triangle is %f\n", Triangle);

  return 0;

}

int

Calculate_Square (int Number)

{

  return Number * Number;

}

float

Calculate_Perimeter_Triangle (float a, float b, float c)

{

  float s, Perimeter;

  Perimeter = a + b + c;

  return Perimeter;

}

float

Calculate_Grade (float g1, float g2, float g3, float g4){

}
0 votes
answered Nov 26, 2022 by Rakesh Hazra (140 points)
Just rename your main() to Main() and _main() and add these to your program and call them accordingly. In C, function names are case sensitive and therefore main and Main would be different, since in C you can use only one main function.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...