Utilizor
Contact Us
HomeC TutorialC Function Parameters

CC Function Parameters

Passing data to functions.

C Function Parameters

Information can be passed to functions as parameters. Parameters act as variables inside the function.

Parameters are specified after the function name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

Return Values

The void keyword, used in the previous examples, indicates that the function should not return a value. If you want the function to return a value, you can use a data type (such as int, float, etc.) instead of void, and use the return keyword inside the function.

Examples

With Parameters

c example

Passing name and age.

#include <stdio.h>

void myFunction(char name[], int age) {
  printf("Hello %s. You are %d years old.\n", name, age);
}

int main() {
  myFunction("Liam", 3);
  myFunction("Jenny", 14);
  myFunction("Anja", 30);
  return 0;
}

Return Value

c example

Function returning an integer.

#include <stdio.h>

int myFunction(int x) {
  return 5 + x;
}

int main() {
  printf("Result: %d", myFunction(3));
  return 0;
}

Sum of Two

c example

Simple recursion example.

#include <stdio.h>

int sum(int k) {
  if (k > 0) {
    return k + sum(k - 1);
  } else {
    return 0;
  }
}

int main() {
  int result = sum(10);
  printf("%d", result);
  return 0;
}

Test Your Knowledge

1. What is a parameter?

2. Which keyword returns a value from a function?