+ 1
How to use Functions in C ?? Need help for an exam
HI, I'm a beginner in C. I'm about to have an exam soon but I really don't understand how to use functions. It's seems so strange to me. Like how do you choose your parameters ?? I really don't understand, could you help me please ? I also don't really understand how arrays and caracters works but functions are worse. Thank you for your help ! Sorry for my bad English, that's not my native language
3 ответов
+ 2
Clara as you write programs you may notice that similar groupings of code lines arise over and over, though one or a few values might be different each time.
One of the reasons to make a function is to avoid retyping those lines. Put the lines into a function and define the few changing values as function parameters. Now instead of retyping code in the main program, just call the function and pass the particular values. Functions let you make shorter programs.
Also they make code easier to maintain. Future changes to the logic are made in one place - the function definition, instead of multiple places in the main code.
+ 1
Your English is fine and welcome to Sololearn!
I like to think of parameters as either ingredients in a recipe or as items in a game.
Ingredients:
You have your bowl, what do you put in your bowl? How much?
public bowlClass (String ingredients, integer howMuch)
Game:
How many coins do you need to pass the level?
public levelSuccess(integer coinsAmount)
Both of those could work.
Perhaps searching the forum, the code playground and the internet will help you with your understanding (it's a good skill to have)
https://sololearn.com/compiler-playground/cQOiBe6hQU33/?ref=app
https://sololearn.com/compiler-playground/c1QLngEo40X1/?ref=app
https://sololearn.com/compiler-playground/W0uW3Wks8UBk/?ref=app
https://www.sololearn.com/discuss/2116829/?ref=app
https://www.sololearn.com/discuss/2126990/?ref=app
0
Welcome to the world of C programming! , Understanding functions is a great step forward. , Think of functions as little machines that perform specific tasks. , Parameters are like the ingredients you give to these machines to get the job done. , , Imagine you're cooking: , - **Ingredients (Parameters):** What do you need to add to your bowl? , Maybe sugar and flour. , In programming, these are the inputs your function needs. , - **Recipe (Function):** The steps you follow to bake a cake. , In C, this is the code inside your function that processes the inputs. , , Here's a simple example: , , ```c , int add(int a, int b) { , return a + b; , } , ``` , , In this function, `a` and `b` are parameters. , When you call `add(3, 4)`, it returns `7`. , , Remember, practice makes perfect. , Try writing small functions and gradually increase complexity. , Good luck with your exam!