0% found this document useful (0 votes)
22 views8 pages

C 31

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

C 31

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
‘11212028 20.49 (C Funetion Parameters 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools C Function Parameters [crv] Looe | 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{]) { jello %s\n", name) ; printf(” ? int main() { nitpsdlwwwavdschools.comiele_funetions_parameters.php ve ‘11212028 20.49 (C Funetion Parameters 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools // 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 Lian, 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. nitpsdimwwavdschools comiele_funetions_parameters.ohp 28 ‘11212028 20.49 (C Funetion Parameters 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools 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. Pass Arrays as Function Parameters You can also pass arrays to a function: Example void myFunction(int myNumbers[5]) { for (int i=; i < 55 itt) { printf(“%d\n", myNumbers[i])5 int main() { int myNumbers[S] = (10, 2, 30, 40, 50}; myFunction(myNumbers) ; return 0; Example Explained The function ( myFunction ) takes an array as its parameter (int myNumbers[5] ), and loops through the array elements with the for loop. When the function is called inside main() , we pass along the myNumbers array, which outputs the array elements. nitpsdimwwavdschools comiele_funetions_parameters.ohp 38 ‘11212028 20.49 (C Funetion Parameters 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools 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))5 return @; // Outputs 8 (5 + 3) This example returns the sum of a function with two parameters: Example int myFunction(int x, int y) { return x + y3 > int main() { printf("Result is: %d", myFunction(5, 3)); return 03 nitpsdlwwwavdschools.comiele_funetions_parameters.php 48 ‘11212028 20.49 (C Funetion Parameters 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools You can also store the result in a variable: Example int myFunction(int x, int y) { return x + y5 int main() { int result = myFunction(s, 3); printf("Result is = %d", result); return 0; ? // Outputs 8 (5 + 3) Real-Life Example To demonstrate a practical example of using functions, let's create a program that converts a value from fahrenheit to celsius: Example // Function to convert Fahrenheit to Celsius float toCelsius(float fahrenheit) { return (5.0 / 9.8) * (fahrenheit - 32.0); int main() { // Set a fahrenheit value nitpsdlwwwavdschools.comiele_funetions_parameters.php 58 ‘11212028 20.49 ¢ Function Parameters 3 w Tutorials» Exercisese Servieesey§ QO SignUp Login schools = CSS JAVASCRIPT SQL. PYTHON = JAVA. PHP-—«S HOWTO— W3.CSS_— // Print the fahrenheit value printf("Fahrenheit: %.2f\n", f_value); // Print the result printf("Convert Fahrenheit to Celsius: %.2f\n", result); return 3 Try it Yourself » Log in to track progress J & Earn Your Certificate Today And Unlock Tomorrow's Opportunities! Celt SeTacls) Don't let hesitation hold you back. a Theest time to starts now, COLOR PICKER htipsulmaew.wdechools.comicle.functions_parameters.pha oe ‘11212028 20.49 w schools Tutorials~ Exercises = CSS JAVASCRIPT = SQL ¢ Function Parameters Sevicesy Q @O Sign Up. PYTHON JAVA PHP = HOWTO w schools SPACES GET CERTIFIED Top Tutorials HTML Tutorial SS Tutorial JavaScript Tutorial How To Tutorial ‘SQL Tutorial Python Tutorial Wa.cSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial cH Tutorial Query Tutorial Top References HTML Reference CSS Reference JavaScript Reference ‘SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference ‘Angular Reference JQuery Reference Top Examples HTML Examples CSS Examples JavaScript Examples How To Examples SQL Examples Python Examples W3.CSS Examples Bootstrap Examples htipsulmaew.wdechools.comicle.functions_parameters.pha UPGRADE NEWSLETTER REPORT ERROR Get Certified HTML Certificate css certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate Query Certificate Login W3.css c 718 ‘11212028 20.49 ¢ Function Parameters 3 WW tutoriatse Exercises» Servicesey Q @ schools SignUp Login = CSS JAVASCRIPT SQL. PYTHON = JAVA. PHP-—«S« HOWTO _—W3.CSS. G@ @& ® © Forum asour \W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSs. htipsulmaew.wdechools.comicle.functions_parameters.pha 8

You might also like