Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

C Function Parameters

Parameters and Arguments

Data can be conveyed to functions via parameters, which function as variables within the function itself.

Parameters are delineated after the function name, residing within the parentheses. You can include multiple parameters, separating them with commas as needed.

Syntax

returnType functionName(parameter1, parameter2, parameter3) {
  // code to be executed
}

The subsequent function accepts a character string named “name” as a parameter. Upon calling the function, we provide a name, which is then utilized within the function to output “Hello” along with the provided name for each individual.

Example

void myFunction(char name[]) {
  printf(“Hello %s\n”, name);
}

int main() {
  myFunction(“Liam”);
  myFunction(“Jenny”);
  myFunction(“Anja”);
  return 0;
}

// Hello Liam
// Hello Jenny
// Hello Anja

 

When a value is passed to a function, it is referred to as an argument. Thus, in the example mentioned earlier: “name” represents a parameter, whereas “Liam,” “Jenny,” and “Anja” serve as arguments.

Multiple Parameters

Within the function, you can include any number of parameters as needed.

Example

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;
}

// Hello Liam. You are 3 years old.
// Hello Jenny. You are 14 years old.
// Hello Anja. You are 30 years old.

 

Keep in mind that when dealing with multiple parameters, the function call should contain an equal number of arguments as there are parameters, and these arguments must be passed in the same sequence.

Pass Arrays as Function Parameters

Arrays can also be passed as arguments to a function.

Example

void myFunction(int myNumbers[5]) {
  for (int i = 0; i < 5; i++) {
    printf(“%d\n”, myNumbers[i]);
  }
}

int main() {
  int myNumbers[5] = {10, 20, 30, 40, 50};
  myFunction(myNumbers);
  return 0;
}

 

Example Explained

The function, myFunction, accepts an array as its parameter (int myNumbers[5]), and iterates through the array elements using a for loop.

Upon calling the function within main(), we provide the myNumbers array, resulting in the output of its elements.

Keep in mind that when calling the function, you only need to utilize the name of the array as an argument, like so: myFunction(myNumbers). However, in the function parameter, the complete declaration of the array is required (int myNumbers[5]).

Return Values

The void keyword, as demonstrated in the previous examples, signifies that the function is not intended to return a value. Should you desire the function to yield a value, you can specify a data type ( e.g., int or float ) instead of void and employ the return keyword within the function.

Example

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

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

// Outputs 8 (5 + 3)

This example illustrates the function returning the sum of two parameters.

Example

int myFunction(int x, int y) {
  return x + y;
}

int main() {
  printf(“Result is: %d”, myFunction(5, 3));
  return 0;
}

// Outputs 8 (5 + 3)

Alternatively, you can assign the result to a variable.

Example

int myFunction(int x, int y) {
  return x + y;
}

int main() {
  int result = myFunction(5, 3);
  printf(“Result is = %d”, result);
  return 0;
}
// Outputs 8 (5 + 3)

Real-Life Example

To illustrate a practical application of functions, let’s develop a program for converting a temperature value from Fahrenheit to Celsius.

Example

// Function to convert Fahrenheit to Celsius
float toCelsius(float fahrenheit) {
  return (5.0 / 9.0) * (fahrenheit – 32.0);
}

int main() {
  // Set a fahrenheit value
  float f_value = 98.8;

  // Call the function with the fahrenheit value
  float result = toCelsius(f_value);

  // Print the fahrenheit value
  printf(“Fahrenheit: %.2f\n”, f_value);

  // Print the result
  printf(“Convert Fahrenheit to Celsius: %.2f\n”, result);

  return 0;
}