C Tutorial

C Functions

A function has a return type, a name, and a body. Call it to reuse work without copy-paste.

A named piece of work

You already use functions: main is one, and printf is another. A function packages a block of statements behind a name. You write the steps once. You call the name whenever you need those steps.

Every function has a return type (what it hands back, or void if it hands back nothing), a name, parentheses, and a body. Execution of a program still starts at main. Other functions run only when something calls them. This course writes int main(void).

Return a value

Put helpers above main so the compiler has already seen the full definition whenmain calls them. The return type on the left must match what return sends back.

Example

#include <stdio.h>

double square(double x) {
  return x * x;
}

int main(void) {
  printf("%.1f\n", square(3.0));
  double area = square(5.0);
  printf("%.1f\n", area);
  return 0;
}

square(3.0) is a call. The argument 3.0 becomes the parameter x inside the function. When return runs, the call is replaced by the value 9.0. You can print that value or store it.

void means no value back

A void function does work — usually printing — and then stops. There is no returnvalue to assign. You can write a bare return; to leave early. You cannot writeint n = greet();.

Example

#include <stdio.h>

void greet(const char *name) {
  printf("hello, %s\n", name);
}

int main(void) {
  greet("Mina");
  greet("Omar");
  return 0;
}

Two calls, one definition. Change the message in greet and every call site updates. That is the whole reason to extract a function: one place to fix, not six pasted copies.

Prototype versus definition

A definition is the function with a body. A prototype (a declaration) is the return type, the name, and the parameter list, ending with a semicolon and no body. The compiler only needs a prototype before the first call. The definition can sit later in the file.

Example

#include <stdio.h>

int max_of(int a, int b);

int main(void) {
  printf("%d\n", max_of(4, 9));
  printf("%d\n", max_of(12, 3));
  return 0;
}

int max_of(int a, int b) {
  if (a > b) {
    return a;
  }
  return b;
}

The prototype above main promises that max_of exists. The definition belowmain keeps the file readable when helpers grow. The parameter names in the prototype are optional; the types are not. They must match the definition.

Call order and one main

C reads the file top to bottom. If you call a function the compiler has never seen, you get an error. Two legal layouts:

  • Define helpers first, then main.
  • Declare prototypes first, then main, then define the helpers.

A program you run has exactly one main. Helpers return to whoever called them. mainreturns to the operating system. Keep using return 0; at the end of main.

The return type is not optional. If you forget it, gcc will not accept modern C. void is the type that means “returns nothing”. Old implicit-int style is not C17.

What belongs in a function

Give a function one job and a name that says that job: square, greet,max_of. If you cannot name it without the word “and”, it is probably two functions. Leave program setup in main and factor the repeated pieces.

Example

#include <stdio.h>

int square(int n) {
  return n * n;
}

int cube(int n) {
  return n * n * n;
}

int main(void) {
  for (int i = 1; i <= 4; i++) {
    printf("%d squared %d, cubed %d\n", i, square(i), cube(i));
  }
  return 0;
}

Open these in /c/try. That is the C editor: gcc, C17, printf.

Next: how arguments get into the function — copies, and pointers that mutate the caller.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Physics

Kinetic energy as a function

Once you have written ½ m v² twice, it should be a function. 2 kg at 3 m/s is 9 J. Call the same function for a 0.05 kg dart and you cannot paste the 2 and the 3 by accident.

The return type is double because joules are not integers except by luck.

KE = ½ m v²

Example

#include <stdio.h>

double kinetic_energy(double mass, double speed) {
  return 0.5 * mass * speed * speed;
}

int main(void) {
  printf("KE = %.1f J\n", kinetic_energy(2.0, 3.0));
  return 0;
}

Maths

Area of a circle

A = πr². For r = 2, A ≈ 12.566. The function is the formula from the exam paper. Change r in main; do not copy the π line again.

A = π r²

Example

#include <stdio.h>

double circle_area(double r) {
  const double PI = 3.14159;
  return PI * r * r;
}

int main(void) {
  printf("A = %.3f\n", circle_area(2.0));
  return 0;
}

FAQ: C Functions

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c functions examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c functions in this C C lesson (C Functions).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.