Class 6 Exam  >  Class 6 Notes  >  C Programming for Beginners  >  C Functions & C Function Parameters

C Functions & C Function Parameters | C Programming for Beginners - Class 6 PDF Download

A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are important for reusing code: Define the code once, and use it many times.

Predefined Functions

So it turns out you already know what a function is. You have been using it the whole time while studying this tutorial!
For example, main() is a function, which is used to execute code, and printf() is a function; used to output/print text to the screen:
Example

int main() {

  printf("Hello World!");

  return 0;

}

Create a Function

To create (often referred to as declare) your own function, specify the name of the function, followed by parentheses () and curly brackets {}:

Syntax

void myFunction() {

  // code to be executed

}

Example Explained

  • myFunction() is the name of the function
  • void means that the function does not have a return value. You will learn more about return values later in the next chapter
  • Inside the function (the body), add code that defines what the function should do

Call a Function

Declared functions are not executed immediately. They are "saved for later use", and will be executed when they are called.
To call a function, write the function's name followed by two parentheses () and a semicolon ;
In the following example, myFunction() is used to print a text (the action), when it is called:
Example
Inside main, call myFunction():

// Create a function

void myFunction() {

  printf("I just got executed!");

}


int main() {

  myFunction(); // call the function

  return 0;

}


// Outputs "I just got executed!"

A function can be called multiple times:
Example

void myFunction() {

  printf("I just got executed!");

}


int main() {

  myFunction();

  myFunction();

  myFunction();

  return 0;

}


// I just got executed!

// I just got executed!

// I just got executed!

C Function Parameters

Parameters and Arguments

Information can be passed to functions as a parameter. 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:

Syntax

returnType functionName(parameter1, parameter2, parameter3) {

  // code to be executed

}

The following function that takes a string of characters with name as parameter. When the function is called, we pass along a name, which is used inside the function to print "Hello" and the name of each person.
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 parameter is passed to the function, it is called an argument. So, from the example above: name is a parameter, while Liam, Jenny and Anja are arguments.

Multiple Parameters

Inside the function, you can add as many parameters as you want:
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.

Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.

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 or float, etc.) instead of void, and use the return keyword inside 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 returns the sum of a function with 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)

You can also store the result in 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)

The document C Functions & C Function Parameters | C Programming for Beginners - Class 6 is a part of the Class 6 Course C Programming for Beginners.
All you need of Class 6 at this link: Class 6
10 videos|13 docs|15 tests

Top Courses for Class 6

10 videos|13 docs|15 tests
Download as PDF
Explore Courses for Class 6 exam

Top Courses for Class 6

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

shortcuts and tricks

,

practice quizzes

,

Semester Notes

,

Free

,

past year papers

,

Exam

,

MCQs

,

Important questions

,

C Functions & C Function Parameters | C Programming for Beginners - Class 6

,

Objective type Questions

,

Viva Questions

,

C Functions & C Function Parameters | C Programming for Beginners - Class 6

,

video lectures

,

study material

,

Sample Paper

,

Summary

,

mock tests for examination

,

C Functions & C Function Parameters | C Programming for Beginners - Class 6

,

ppt

,

Previous Year Questions with Solutions

,

pdf

,

Extra Questions

;