Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Arguments | Functions
C Basics
course content

Course Content

C Basics

C Basics

1. Introduction
2. Data
3. Operators
4. Control Statements
5. Functions
6. Pointers

book
Arguments

Functions can be thought of as mini-factories, taking raw materials and transforming them into various products. Similarly, functions process input data. The input for a function is termed as arguments or parameters.

Recalling the structure of a function:

arguments_placeholder is the spot where you list the function's arguments.

As an example, we'll design a function to convert degrees to radians:

c

Main

copy
1234567891011121314151617181920
#include <stdio.h> // my function for conversion degrees to radians double degreesToRadians(double degree) // *double degree* is argument { double rads = (degree * 3.14) / 180; return rads; // output of function } int main() { double degree = 30; // We immediately print the result of our function printf("30 degrees equals %.2f radians\n", degreesToRadians(degree)); return 0; }

Scope

Imagine a function call as a portal into a separate, self-contained realm where specific actions take place. Objects birthed in this realm exist solely there, and once the realm ceases to exist, so do they.

For instance, if we declare a variable inside a function, it remains inaccessible from outside that function. The sole piece of information we can extract from a function's execution is what we retrieve using the return statement.

Arguments

Functions can take a variety of arguments: basic data types (like int, char, double), arrays/strings, pointers (teaser!), and even other functions.

It's also important to note that the names of the arguments within the function might differ from the actual data you're passing into it.

The variable int inputVar lives only within the scope of the function, acting as a placeholder for the data we want to feed into the function. However, the data types of both the arguments and the actual data passed to the function must align.

Now, let's script a function that pinpoints the largest element in an array:

c

Main

copy
123456789101112131415161718192021222324252627
#include <stdio.h> int findMaxElement(int arr[]) // 1 { int max = arr[0]; // 2 for (int i = 1; i < 5; i++) // 3 { if (arr[i] > max) // 3 { max = arr[i]; // 4 } } return max; } int main() { int array[] = { 10, 5, 7, 14, 3 }; int maxElement = findMaxElement(array); printf("Max element is: %d\n", maxElement); return 0; }

Here's the algorithm:

  1. Pass the array into the function;
  2. The function labels any chosen array element as "max" (regardless of its actual value);
  3. A loop within the function then examines every element, contrasting it with "max";
  4. If any new element is bigger than "max", then that element takes the "max" title.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 3
We're sorry to hear that something went wrong. What happened?
some-alt